1 import { Component, EventEmitter, OnInit, Output } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
4 import { NotificationsService } from 'angular2-notifications'
5 import { VideoPrivacy, VideoUpdate } from '../../../../../shared/models/videos'
6 import { AuthService, ServerService } from '../../core'
7 import { VideoService } from '../../shared/video/video.service'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
10 import { VideoImportService } from '@app/shared/video-import'
11 import { VideoEdit } from '@app/shared/video/video-edit.model'
12 import { LoadingBarService } from '@ngx-loading-bar/core'
13 import { VideoCaptionService } from '@app/shared/video-caption'
14 import { VideoSend } from '@app/videos/+video-edit/shared/video-send'
17 selector: 'my-video-import',
18 templateUrl: './video-import.component.html',
20 './shared/video-edit.component.scss',
21 './video-import.component.scss'
24 export class VideoImportComponent 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.PRIVATE
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.importVideo(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,
90 this.hydrateFormFromVideo()
94 this.loadingBar.complete()
95 this.isImportingVideo = false
96 this.notificationsService.error(this.i18n('Error'), err.message)
101 updateSecondStep () {
102 if (this.checkForm() === false) {
106 this.video.patch(this.form.value)
108 this.isUpdatingVideo = true
111 this.updateVideoAndCaptions(this.video)
114 this.isUpdatingVideo = false
115 this.notificationsService.success(this.i18n('Success'), this.i18n('Video to import updated.'))
117 this.router.navigate([ '/my-account', 'video-imports' ])
121 this.isUpdatingVideo = false
122 this.notificationsService.error(this.i18n('Error'), err.message)
129 private hydrateFormFromVideo () {
130 this.form.patchValue(this.video.toFormPatch())