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'
17 selector: 'my-video-import-url',
18 templateUrl: './video-import-url.component.html',
20 '../shared/video-edit.component.scss',
24 export class VideoImportUrlComponent extends VideoSend implements OnInit, CanComponentDeactivate {
25 @Output() firstStepDone = new EventEmitter<string>()
26 @Output() firstStepError = new EventEmitter<void>()
30 isImportingVideo = false
31 hasImportedVideo = false
32 isUpdatingVideo = false
37 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
40 protected formValidatorService: FormValidatorService,
41 protected loadingBar: LoadingBarService,
42 protected notifier: Notifier,
43 protected authService: AuthService,
44 protected serverService: ServerService,
45 protected videoService: VideoService,
46 protected videoCaptionService: VideoCaptionService,
47 private router: Router,
48 private videoImportService: VideoImportService,
59 return { canDeactivate: true }
63 return this.targetUrl && this.targetUrl.match(/https?:\/\//)
67 this.isImportingVideo = true
69 const videoUpdate: VideoUpdate = {
70 privacy: this.firstStepPrivacyId,
71 waitTranscoding: false,
72 commentsEnabled: true,
73 downloadEnabled: true,
74 channelId: this.firstStepChannelId
77 this.loadingBar.start()
79 this.videoImportService.importVideoUrl(this.targetUrl, videoUpdate).subscribe(
81 this.loadingBar.complete()
82 this.firstStepDone.emit(res.video.name)
83 this.isImportingVideo = false
84 this.hasImportedVideo = true
86 this.video = new VideoEdit(Object.assign(res.video, {
87 commentsEnabled: videoUpdate.commentsEnabled,
88 downloadEnabled: videoUpdate.downloadEnabled,
94 this.hydrateFormFromVideo()
98 this.loadingBar.complete()
99 this.isImportingVideo = false
100 this.firstStepError.emit()
101 this.notifier.error(err.message)
106 updateSecondStep () {
107 if (this.checkForm() === false) {
111 this.video.patch(this.form.value)
113 this.isUpdatingVideo = true
116 this.updateVideoAndCaptions(this.video)
119 this.isUpdatingVideo = false
120 this.notifier.success(this.i18n('Video to import updated.'))
122 this.router.navigate([ '/my-account', 'video-imports' ])
126 this.error = err.message
134 private hydrateFormFromVideo () {
135 this.form.patchValue(this.video.toFormPatch())