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 { VideoSend } from './video-send'
9 import { LoadingBarService } from '@ngx-loading-bar/core'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
11 import { VideoPrivacy, VideoUpdate } from '@shared/models'
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, 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,
56 return { canDeactivate: true }
60 return this.targetUrl && this.targetUrl.match(/https?:\/\//)
64 this.isImportingVideo = true
66 const videoUpdate: VideoUpdate = {
67 privacy: this.firstStepPrivacyId,
68 waitTranscoding: false,
69 commentsEnabled: true,
70 downloadEnabled: true,
71 channelId: this.firstStepChannelId
74 this.loadingBar.useRef().start()
76 this.videoImportService
77 .importVideoUrl(this.targetUrl, videoUpdate)
80 return this.videoCaptionService
81 .listCaptions(res.video.id)
83 map(result => ({ video: res.video, videoCaptions: result.data }))
88 ({ video, videoCaptions }) => {
89 this.loadingBar.useRef().complete()
90 this.firstStepDone.emit(video.name)
91 this.isImportingVideo = false
92 this.hasImportedVideo = true
94 const absoluteAPIUrl = getAbsoluteAPIUrl()
96 const thumbnailUrl = video.thumbnailPath
97 ? absoluteAPIUrl + video.thumbnailPath
100 const previewUrl = video.previewPath
101 ? absoluteAPIUrl + video.previewPath
104 this.video = new VideoEdit(Object.assign(video, {
105 commentsEnabled: videoUpdate.commentsEnabled,
106 downloadEnabled: videoUpdate.downloadEnabled,
112 this.videoCaptions = videoCaptions
114 this.hydrateFormFromVideo()
118 this.loadingBar.useRef().complete()
119 this.isImportingVideo = false
120 this.firstStepError.emit()
121 this.notifier.error(err.message)
126 updateSecondStep () {
127 if (this.checkForm() === false) {
131 this.video.patch(this.form.value)
133 this.isUpdatingVideo = true
136 this.updateVideoAndCaptions(this.video)
139 this.isUpdatingVideo = false
140 this.notifier.success(this.i18n('Video to import updated.'))
142 this.router.navigate([ '/my-account', 'video-imports' ])
146 this.error = err.message
154 private hydrateFormFromVideo () {
155 this.form.patchValue(this.video.toFormPatch())
160 name: 'thumbnailfile'
168 for (const obj of objects) {
169 fetch(this.video[obj.url])
170 .then(response => response.blob())
172 this.form.patchValue({