1 import { Component, EventEmitter, OnInit, Output } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { VideoPrivacy, VideoUpdate } from '../../../../../../shared/models/videos'
4 import { AuthService, Notifier, ServerService } from '../../../core'
5 import { VideoService } from '../../../shared/video/video.service'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { LoadingBarService } from '@ngx-loading-bar/core'
8 import { VideoSend } from '@app/videos/+video-edit/video-add-components/video-send'
9 import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
10 import { VideoEdit } from '@app/shared/video/video-edit.model'
11 import { FormValidatorService } from '@app/shared'
12 import { VideoCaptionService } from '@app/shared/video-caption'
13 import { VideoImportService } from '@app/shared/video-import'
14 import { scrollToTop } from '@app/shared/misc/utils'
15 import { switchMap, map } from 'rxjs/operators'
18 selector: 'my-video-import-url',
19 templateUrl: './video-import-url.component.html',
21 '../shared/video-edit.component.scss',
25 export class VideoImportUrlComponent extends VideoSend implements OnInit, CanComponentDeactivate {
26 @Output() firstStepDone = new EventEmitter<string>()
27 @Output() firstStepError = new EventEmitter<void>()
31 isImportingVideo = false
32 hasImportedVideo = false
33 isUpdatingVideo = false
38 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
41 protected formValidatorService: FormValidatorService,
42 protected loadingBar: LoadingBarService,
43 protected notifier: Notifier,
44 protected authService: AuthService,
45 protected serverService: ServerService,
46 protected videoService: VideoService,
47 protected videoCaptionService: VideoCaptionService,
48 private router: Router,
49 private videoImportService: VideoImportService,
60 return { canDeactivate: true }
64 return this.targetUrl && this.targetUrl.match(/https?:\/\//)
68 this.isImportingVideo = true
70 const videoUpdate: VideoUpdate = {
71 privacy: this.firstStepPrivacyId,
72 waitTranscoding: false,
73 commentsEnabled: true,
74 downloadEnabled: true,
75 channelId: this.firstStepChannelId
78 this.loadingBar.start()
80 this.videoImportService
81 .importVideoUrl(this.targetUrl, videoUpdate)
84 return this.videoCaptionService
85 .listCaptions(res.video.id)
87 map(result => ({ video: res.video, videoCaptions: result.data }))
92 ({ video, videoCaptions }) => {
93 this.loadingBar.complete()
94 this.firstStepDone.emit(video.name)
95 this.isImportingVideo = false
96 this.hasImportedVideo = true
98 this.video = new VideoEdit(Object.assign(video, {
99 commentsEnabled: videoUpdate.commentsEnabled,
100 downloadEnabled: videoUpdate.downloadEnabled,
106 this.videoCaptions = videoCaptions
108 this.hydrateFormFromVideo()
112 this.loadingBar.complete()
113 this.isImportingVideo = false
114 this.firstStepError.emit()
115 this.notifier.error(err.message)
120 updateSecondStep () {
121 if (this.checkForm() === false) {
125 this.video.patch(this.form.value)
127 this.isUpdatingVideo = true
130 this.updateVideoAndCaptions(this.video)
133 this.isUpdatingVideo = false
134 this.notifier.success(this.i18n('Video to import updated.'))
136 this.router.navigate([ '/my-account', 'video-imports' ])
140 this.error = err.message
148 private hydrateFormFromVideo () {
149 this.form.patchValue(this.video.toFormPatch())