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'
17 selector: 'my-video-import-url',
18 templateUrl: './video-import-url.component.html',
20 '../shared/video-edit.component.scss',
21 './video-import-url.component.scss'
24 export class VideoImportUrlComponent extends VideoSend implements OnInit, CanComponentDeactivate {
25 @Output() firstStepDone = new EventEmitter<string>()
30 isImportingVideo = false
31 hasImportedVideo = false
32 isUpdatingVideo = false
36 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
39 protected formValidatorService: FormValidatorService,
40 protected loadingBar: LoadingBarService,
41 protected notificationsService: NotificationsService,
42 protected authService: AuthService,
43 protected serverService: ServerService,
44 protected videoService: VideoService,
45 protected videoCaptionService: VideoCaptionService,
46 private router: Router,
47 private videoImportService: VideoImportService,
58 return { canDeactivate: true }
62 return this.targetUrl && this.targetUrl.match(/https?:\/\//)
66 this.isImportingVideo = true
68 const videoUpdate: VideoUpdate = {
69 privacy: this.firstStepPrivacyId,
70 waitTranscoding: false,
71 commentsEnabled: true,
72 channelId: this.firstStepChannelId
75 this.loadingBar.start()
77 this.videoImportService.importVideoUrl(this.targetUrl, videoUpdate).subscribe(
79 this.loadingBar.complete()
80 this.firstStepDone.emit(res.video.name)
81 this.isImportingVideo = false
82 this.hasImportedVideo = true
84 this.video = new VideoEdit(Object.assign(res.video, {
85 commentsEnabled: videoUpdate.commentsEnabled,
91 this.videoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies)
93 this.hydrateFormFromVideo()
97 this.loadingBar.complete()
98 this.isImportingVideo = false
99 this.notificationsService.error(this.i18n('Error'), err.message)
104 updateSecondStep () {
105 if (this.checkForm() === false) {
109 this.video.patch(this.form.value)
111 this.isUpdatingVideo = true
114 this.updateVideoAndCaptions(this.video)
117 this.isUpdatingVideo = false
118 this.notificationsService.success(this.i18n('Success'), this.i18n('Video to import updated.'))
120 this.router.navigate([ '/my-account', 'video-imports' ])
124 this.isUpdatingVideo = false
125 this.notificationsService.error(this.i18n('Error'), err.message)
132 private hydrateFormFromVideo () {
133 this.form.patchValue(this.video.toFormPatch())