1 import { Component, EventEmitter, OnInit, Output } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { NotificationsService } from 'angular2-notifications'
4 import { VideoPrivacy, VideoUpdate } from '../../../../../../shared/models/videos'
5 import { AuthService, ServerService } from '../../../core'
6 import { VideoService } from '../../../shared/video/video.service'
7 import { I18n } from '@ngx-translate/i18n-polyfill'
8 import { LoadingBarService } from '@ngx-loading-bar/core'
9 import { VideoSend } from '@app/videos/+video-edit/video-add-components/video-send'
10 import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
11 import { VideoEdit } from '@app/shared/video/video-edit.model'
12 import { FormValidatorService } from '@app/shared'
13 import { VideoCaptionService } from '@app/shared/video-caption'
14 import { VideoImportService } from '@app/shared/video-import'
15 import { scrollToTop } from '@app/shared/misc/utils'
18 selector: 'my-video-import-url',
19 templateUrl: './video-import-url.component.html',
21 '../shared/video-edit.component.scss',
22 './video-import-url.component.scss'
25 export class VideoImportUrlComponent extends VideoSend implements OnInit, CanComponentDeactivate {
26 @Output() firstStepDone = new EventEmitter<string>()
27 @Output() firstStepError = new EventEmitter<void>()
31 isImportingVideo = false
32 hasImportedVideo = false
33 isUpdatingVideo = false
38 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
41 protected formValidatorService: FormValidatorService,
42 protected loadingBar: LoadingBarService,
43 protected notificationsService: NotificationsService,
44 protected authService: AuthService,
45 protected serverService: ServerService,
46 protected videoService: VideoService,
47 protected videoCaptionService: VideoCaptionService,
48 private router: Router,
49 private videoImportService: VideoImportService,
60 return { canDeactivate: true }
64 return this.targetUrl && this.targetUrl.match(/https?:\/\//)
68 this.isImportingVideo = true
70 const videoUpdate: VideoUpdate = {
71 privacy: this.firstStepPrivacyId,
72 waitTranscoding: false,
73 commentsEnabled: 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,
93 this.videoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies)
95 this.hydrateFormFromVideo()
99 this.loadingBar.complete()
100 this.isImportingVideo = false
101 this.firstStepError.emit()
102 this.notificationsService.error(this.i18n('Error'), err.message)
107 updateSecondStep () {
108 if (this.checkForm() === false) {
112 this.video.patch(this.form.value)
114 this.isUpdatingVideo = true
117 this.updateVideoAndCaptions(this.video)
120 this.isUpdatingVideo = false
121 this.notificationsService.success(this.i18n('Success'), this.i18n('Video to import updated.'))
123 this.router.navigate([ '/my-account', 'video-imports' ])
127 this.error = err.message
135 private hydrateFormFromVideo () {
136 this.form.patchValue(this.video.toFormPatch())