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