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 { VideoConstant, VideoPrivacy, VideoUpdate } from '../../../../../shared/models/videos'
6 import { AuthService, ServerService } from '../../core'
7 import { FormReactive } from '../../shared'
8 import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
9 import { VideoService } from '../../shared/video/video.service'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
11 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
12 import { VideoCaptionEdit } from '@app/shared/video-caption/video-caption-edit.model'
13 import { VideoImportService } from '@app/shared/video-import'
14 import { VideoEdit } from '@app/shared/video/video-edit.model'
15 import { switchMap } from 'rxjs/operators'
16 import { LoadingBarService } from '@ngx-loading-bar/core'
17 import { VideoCaptionService } from '@app/shared/video-caption'
20 selector: 'my-video-import',
21 templateUrl: './video-import.component.html',
23 './shared/video-edit.component.scss',
24 './video-import.component.scss'
27 export class VideoImportComponent extends FormReactive implements OnInit, CanComponentDeactivate {
28 @Output() firstStepDone = new EventEmitter<string>()
33 isImportingVideo = false
34 hasImportedVideo = false
35 isUpdatingVideo = false
37 userVideoChannels: { id: number, label: string, support: string }[] = []
38 videoPrivacies: VideoConstant<string>[] = []
39 videoCaptions: VideoCaptionEdit[] = []
41 firstStepPrivacyId = 0
42 firstStepChannelId = 0
46 protected formValidatorService: FormValidatorService,
47 private router: Router,
48 private loadingBar: LoadingBarService,
49 private notificationsService: NotificationsService,
50 private authService: AuthService,
51 private serverService: ServerService,
52 private videoService: VideoService,
53 private videoImportService: VideoImportService,
54 private videoCaptionService: VideoCaptionService,
63 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
64 .then(() => this.firstStepChannelId = this.userVideoChannels[ 0 ].id)
66 this.serverService.videoPrivaciesLoaded
69 this.videoPrivacies = this.serverService.getVideoPrivacies()
72 this.firstStepPrivacyId = VideoPrivacy.PRIVATE
77 return { canDeactivate: true }
83 return this.form.valid
87 return this.targetUrl && this.targetUrl.match(/https?:\/\//)
91 this.isImportingVideo = true
93 const videoUpdate: VideoUpdate = {
94 privacy: this.firstStepPrivacyId,
95 waitTranscoding: false,
96 commentsEnabled: true,
97 channelId: this.firstStepChannelId
100 this.loadingBar.start()
102 this.videoImportService.importVideo(this.targetUrl, videoUpdate).subscribe(
104 this.loadingBar.complete()
105 this.firstStepDone.emit(res.video.name)
106 this.isImportingVideo = false
107 this.hasImportedVideo = true
109 this.video = new VideoEdit(Object.assign(res.video, {
110 commentsEnabled: videoUpdate.commentsEnabled,
115 this.hydrateFormFromVideo()
119 this.loadingBar.complete()
120 this.isImportingVideo = false
121 this.notificationsService.error(this.i18n('Error'), err.message)
126 updateSecondStep () {
127 if (this.checkForm() === false) {
131 this.video.patch(this.form.value)
133 this.loadingBar.start()
134 this.isUpdatingVideo = true
137 this.videoService.updateVideo(this.video)
139 // Then update captions
140 switchMap(() => this.videoCaptionService.updateCaptions(this.video.id, this.videoCaptions))
144 this.isUpdatingVideo = false
145 this.loadingBar.complete()
146 this.notificationsService.success(this.i18n('Success'), this.i18n('Video to import updated.'))
148 this.router.navigate([ '/my-account', 'video-imports' ])
152 this.loadingBar.complete()
153 this.isUpdatingVideo = false
154 this.notificationsService.error(this.i18n('Error'), err.message)
161 private hydrateFormFromVideo () {
162 this.form.patchValue(this.video.toFormPatch())