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 { 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
35 protected formValidatorService: FormValidatorService,
36 protected loadingBar: LoadingBarService,
37 protected notifier: Notifier,
38 protected authService: AuthService,
39 protected serverService: ServerService,
40 protected videoService: VideoService,
41 protected videoCaptionService: VideoCaptionService,
42 private router: Router,
43 private videoImportService: VideoImportService,
44 private hooks: HooksService
54 this.hooks.runAction('action:video-url-import.init', 'video-edit')
58 return { canDeactivate: true }
62 return this.targetUrl?.match(/https?:\/\//)
66 this.isImportingVideo = true
68 const videoUpdate: VideoUpdate = {
69 privacy: this.highestPrivacy,
70 waitTranscoding: false,
71 commentsEnabled: true,
72 downloadEnabled: true,
73 channelId: this.firstStepChannelId
76 this.loadingBar.useRef().start()
78 this.videoImportService
79 .importVideoUrl(this.targetUrl, videoUpdate)
82 return this.videoCaptionService
83 .listCaptions(res.video.id)
85 map(result => ({ video: res.video, videoCaptions: result.data }))
90 next: ({ video, videoCaptions }) => {
91 this.loadingBar.useRef().complete()
92 this.firstStepDone.emit(video.name)
93 this.isImportingVideo = false
94 this.hasImportedVideo = true
96 const absoluteAPIUrl = getAbsoluteAPIUrl()
98 const thumbnailUrl = video.thumbnailPath
99 ? absoluteAPIUrl + video.thumbnailPath
102 const previewUrl = video.previewPath
103 ? absoluteAPIUrl + video.previewPath
106 this.video = new VideoEdit(Object.assign(video, {
107 commentsEnabled: videoUpdate.commentsEnabled,
108 downloadEnabled: videoUpdate.downloadEnabled,
109 privacy: { id: this.firstStepPrivacyId },
115 this.videoCaptions = videoCaptions
117 hydrateFormFromVideo(this.form, this.video, true)
121 this.loadingBar.useRef().complete()
122 this.isImportingVideo = false
123 this.firstStepError.emit()
124 this.notifier.error(err.message)
129 updateSecondStep () {
130 if (this.checkForm() === false) {
134 this.video.patch(this.form.value)
136 this.isUpdatingVideo = true
139 this.updateVideoAndCaptions(this.video)
142 this.isUpdatingVideo = false
143 this.notifier.success($localize`Video to import updated.`)
145 this.router.navigate([ '/my-library', 'video-imports' ])
149 this.error = err.message