1 import { map, switchMap } from 'rxjs/operators'
2 import { AfterViewInit, Component, EventEmitter, OnInit, Output } from '@angular/core'
3 import { Router } from '@angular/router'
4 import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core'
5 import { getAbsoluteAPIUrl, scrollToTop } from '@app/helpers'
6 import { FormValidatorService } from '@app/shared/shared-forms'
7 import { VideoCaptionService, VideoEdit, VideoImportService, VideoService } from '@app/shared/shared-main'
8 import { LoadingBarService } from '@ngx-loading-bar/core'
9 import { VideoPrivacy, VideoUpdate } from '@shared/models'
10 import { hydrateFormFromVideo } from '../shared/video-edit-utils'
11 import { VideoSend } from './video-send'
14 selector: 'my-video-import-url',
15 templateUrl: './video-import-url.component.html',
17 '../shared/video-edit.component.scss',
21 export class VideoImportUrlComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
22 @Output() firstStepDone = new EventEmitter<string>()
23 @Output() firstStepError = new EventEmitter<void>()
27 isImportingVideo = false
28 hasImportedVideo = false
29 isUpdatingVideo = false
34 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
37 protected formValidatorService: FormValidatorService,
38 protected loadingBar: LoadingBarService,
39 protected notifier: Notifier,
40 protected authService: AuthService,
41 protected serverService: ServerService,
42 protected videoService: VideoService,
43 protected videoCaptionService: VideoCaptionService,
44 private router: Router,
45 private videoImportService: VideoImportService,
46 private hooks: HooksService
56 this.hooks.runAction('action:video-url-import.init', 'video-edit')
60 return { canDeactivate: true }
64 return this.targetUrl && this.targetUrl.match(/https?:\/\//)
68 this.isImportingVideo = true
70 const videoUpdate: VideoUpdate = {
71 privacy: VideoPrivacy.PRIVATE,
72 waitTranscoding: false,
73 commentsEnabled: true,
74 downloadEnabled: true,
75 channelId: this.firstStepChannelId
78 this.loadingBar.useRef().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.useRef().complete()
94 this.firstStepDone.emit(video.name)
95 this.isImportingVideo = false
96 this.hasImportedVideo = true
98 const absoluteAPIUrl = getAbsoluteAPIUrl()
100 const thumbnailUrl = video.thumbnailPath
101 ? absoluteAPIUrl + video.thumbnailPath
104 const previewUrl = video.previewPath
105 ? absoluteAPIUrl + video.previewPath
108 this.video = new VideoEdit(Object.assign(video, {
109 commentsEnabled: videoUpdate.commentsEnabled,
110 downloadEnabled: videoUpdate.downloadEnabled,
111 privacy: { id: this.firstStepPrivacyId },
117 this.videoCaptions = videoCaptions
119 hydrateFormFromVideo(this.form, this.video, true)
123 this.loadingBar.useRef().complete()
124 this.isImportingVideo = false
125 this.firstStepError.emit()
126 this.notifier.error(err.message)
131 updateSecondStep () {
132 if (this.checkForm() === false) {
136 this.video.patch(this.form.value)
138 this.isUpdatingVideo = true
141 this.updateVideoAndCaptions(this.video)
144 this.isUpdatingVideo = false
145 this.notifier.success($localize`Video to import updated.`)
147 this.router.navigate([ '/my-library', 'video-imports' ])
151 this.error = err.message