1 import { map, switchMap } from 'rxjs/operators'
2 import { Component, EventEmitter, OnInit, Output } from '@angular/core'
3 import { Router } from '@angular/router'
4 import { AuthService, CanComponentDeactivate, 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 { VideoSend } from './video-send'
13 selector: 'my-video-import-url',
14 templateUrl: './video-import-url.component.html',
16 '../shared/video-edit.component.scss',
20 export class VideoImportUrlComponent extends VideoSend implements OnInit, CanComponentDeactivate {
21 @Output() firstStepDone = new EventEmitter<string>()
22 @Output() firstStepError = new EventEmitter<void>()
26 isImportingVideo = false
27 hasImportedVideo = false
28 isUpdatingVideo = false
33 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
36 protected formValidatorService: FormValidatorService,
37 protected loadingBar: LoadingBarService,
38 protected notifier: Notifier,
39 protected authService: AuthService,
40 protected serverService: ServerService,
41 protected videoService: VideoService,
42 protected videoCaptionService: VideoCaptionService,
43 private router: Router,
44 private videoImportService: VideoImportService
54 return { canDeactivate: true }
58 return this.targetUrl && this.targetUrl.match(/https?:\/\//)
62 this.isImportingVideo = true
64 const videoUpdate: VideoUpdate = {
65 privacy: this.firstStepPrivacyId,
66 waitTranscoding: false,
67 commentsEnabled: true,
68 downloadEnabled: true,
69 channelId: this.firstStepChannelId
72 this.loadingBar.useRef().start()
74 this.videoImportService
75 .importVideoUrl(this.targetUrl, videoUpdate)
78 return this.videoCaptionService
79 .listCaptions(res.video.id)
81 map(result => ({ video: res.video, videoCaptions: result.data }))
86 ({ video, videoCaptions }) => {
87 this.loadingBar.useRef().complete()
88 this.firstStepDone.emit(video.name)
89 this.isImportingVideo = false
90 this.hasImportedVideo = true
92 const absoluteAPIUrl = getAbsoluteAPIUrl()
94 const thumbnailUrl = video.thumbnailPath
95 ? absoluteAPIUrl + video.thumbnailPath
98 const previewUrl = video.previewPath
99 ? absoluteAPIUrl + video.previewPath
102 this.video = new VideoEdit(Object.assign(video, {
103 commentsEnabled: videoUpdate.commentsEnabled,
104 downloadEnabled: videoUpdate.downloadEnabled,
110 this.videoCaptions = videoCaptions
112 this.hydrateFormFromVideo()
116 this.loadingBar.useRef().complete()
117 this.isImportingVideo = false
118 this.firstStepError.emit()
119 this.notifier.error(err.message)
124 updateSecondStep () {
125 if (this.checkForm() === false) {
129 this.video.patch(this.form.value)
131 this.isUpdatingVideo = true
134 this.updateVideoAndCaptions(this.video)
137 this.isUpdatingVideo = false
138 this.notifier.success($localize`Video to import updated.`)
140 this.router.navigate([ '/my-account', 'video-imports' ])
144 this.error = err.message
152 private hydrateFormFromVideo () {
153 this.form.patchValue(this.video.toFormPatch())
158 name: 'thumbnailfile'
166 for (const obj of objects) {
167 fetch(this.video[obj.url])
168 .then(response => response.blob())
170 this.form.patchValue({