adding missing i18n for schedule option
[oweals/peertube.git] / client / src / app / videos / +video-edit / video-add-components / video-import-torrent.component.ts
1 import { Component, EventEmitter, OnInit, Output, ViewChild } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { NotificationsService } from 'angular2-notifications'
4 import { VideoPrivacy, VideoUpdate } from '../../../../../../shared/models/videos'
5 import { AuthService, ServerService } from '../../../core'
6 import { VideoService } from '../../../shared/video/video.service'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { LoadingBarService } from '@ngx-loading-bar/core'
9 import { VideoSend } from '@app/videos/+video-edit/video-add-components/video-send'
10 import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
11 import { VideoEdit } from '@app/shared/video/video-edit.model'
12 import { FormValidatorService } from '@app/shared'
13 import { VideoCaptionService } from '@app/shared/video-caption'
14 import { VideoImportService } from '@app/shared/video-import'
15
16 @Component({
17   selector: 'my-video-import-torrent',
18   templateUrl: './video-import-torrent.component.html',
19   styleUrls: [
20     '../shared/video-edit.component.scss',
21     './video-import-torrent.component.scss'
22   ]
23 })
24 export class VideoImportTorrentComponent extends VideoSend implements OnInit, CanComponentDeactivate {
25   @Output() firstStepDone = new EventEmitter<string>()
26   @ViewChild('torrentfileInput') torrentfileInput
27
28   videoFileName: string
29   magnetUri = ''
30
31   isImportingVideo = false
32   hasImportedVideo = false
33   isUpdatingVideo = false
34
35   video: VideoEdit
36
37   protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
38
39   constructor (
40     protected formValidatorService: FormValidatorService,
41     protected loadingBar: LoadingBarService,
42     protected notificationsService: NotificationsService,
43     protected authService: AuthService,
44     protected serverService: ServerService,
45     protected videoService: VideoService,
46     protected videoCaptionService: VideoCaptionService,
47     private router: Router,
48     private videoImportService: VideoImportService,
49     private i18n: I18n
50   ) {
51     super()
52   }
53
54   ngOnInit () {
55     super.ngOnInit()
56   }
57
58   canDeactivate () {
59     return { canDeactivate: true }
60   }
61
62   isMagnetUrlValid () {
63     return !!this.magnetUri
64   }
65
66   fileChange () {
67     const torrentfile = this.torrentfileInput.nativeElement.files[0] as File
68     if (!torrentfile) return
69
70     this.importVideo(torrentfile)
71   }
72
73   importVideo (torrentfile?: Blob) {
74     this.isImportingVideo = true
75
76     const videoUpdate: VideoUpdate = {
77       privacy: this.firstStepPrivacyId,
78       waitTranscoding: false,
79       commentsEnabled: true,
80       channelId: this.firstStepChannelId
81     }
82
83     this.loadingBar.start()
84
85     this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate).subscribe(
86       res => {
87         this.loadingBar.complete()
88         this.firstStepDone.emit(res.video.name)
89         this.isImportingVideo = false
90         this.hasImportedVideo = true
91
92         this.video = new VideoEdit(Object.assign(res.video, {
93           commentsEnabled: videoUpdate.commentsEnabled,
94           support: null,
95           thumbnailUrl: null,
96           previewUrl: null
97         }))
98
99         this.videoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies)
100
101         this.hydrateFormFromVideo()
102       },
103
104       err => {
105         this.loadingBar.complete()
106         this.isImportingVideo = false
107         this.notificationsService.error(this.i18n('Error'), err.message)
108       }
109     )
110   }
111
112   updateSecondStep () {
113     if (this.checkForm() === false) {
114       return
115     }
116
117     this.video.patch(this.form.value)
118
119     this.isUpdatingVideo = true
120
121     // Update the video
122     this.updateVideoAndCaptions(this.video)
123         .subscribe(
124           () => {
125             this.isUpdatingVideo = false
126             this.notificationsService.success(this.i18n('Success'), this.i18n('Video to import updated.'))
127
128             this.router.navigate([ '/my-account', 'video-imports' ])
129           },
130
131           err => {
132             this.isUpdatingVideo = false
133             this.notificationsService.error(this.i18n('Error'), err.message)
134             console.error(err)
135           }
136         )
137
138   }
139
140   private hydrateFormFromVideo () {
141     this.form.patchValue(this.video.toFormPatch())
142   }
143 }