]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-add-components/video-import-url.component.ts
Merge branch 'master' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-add-components / video-import-url.component.ts
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
16 @Component({
17 selector: 'my-video-import-url',
18 templateUrl: './video-import-url.component.html',
19 styleUrls: [
20 '../shared/video-edit.component.scss',
21 './video-import-url.component.scss'
22 ]
23 })
24 export class VideoImportUrlComponent extends VideoSend implements OnInit, CanComponentDeactivate {
25 @Output() firstStepDone = new EventEmitter<string>()
26
27 targetUrl = ''
28 videoFileName: string
29
30 isImportingVideo = false
31 hasImportedVideo = false
32 isUpdatingVideo = false
33
34 video: VideoEdit
35
36 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
37
38 constructor (
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,
48 private i18n: I18n
49 ) {
50 super()
51 }
52
53 ngOnInit () {
54 super.ngOnInit()
55 }
56
57 canDeactivate () {
58 return { canDeactivate: true }
59 }
60
61 isTargetUrlValid () {
62 return this.targetUrl && this.targetUrl.match(/https?:\/\//)
63 }
64
65 importVideo () {
66 this.isImportingVideo = true
67
68 const videoUpdate: VideoUpdate = {
69 privacy: this.firstStepPrivacyId,
70 waitTranscoding: false,
71 commentsEnabled: true,
72 channelId: this.firstStepChannelId
73 }
74
75 this.loadingBar.start()
76
77 this.videoImportService.importVideoUrl(this.targetUrl, videoUpdate).subscribe(
78 res => {
79 this.loadingBar.complete()
80 this.firstStepDone.emit(res.video.name)
81 this.isImportingVideo = false
82 this.hasImportedVideo = true
83
84 this.video = new VideoEdit(Object.assign(res.video, {
85 commentsEnabled: videoUpdate.commentsEnabled,
86 support: null,
87 thumbnailUrl: null,
88 previewUrl: null
89 }))
90
91 this.videoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies)
92
93 this.hydrateFormFromVideo()
94 },
95
96 err => {
97 this.loadingBar.complete()
98 this.isImportingVideo = false
99 this.notificationsService.error(this.i18n('Error'), err.message)
100 }
101 )
102 }
103
104 updateSecondStep () {
105 if (this.checkForm() === false) {
106 return
107 }
108
109 this.video.patch(this.form.value)
110
111 this.isUpdatingVideo = true
112
113 // Update the video
114 this.updateVideoAndCaptions(this.video)
115 .subscribe(
116 () => {
117 this.isUpdatingVideo = false
118 this.notificationsService.success(this.i18n('Success'), this.i18n('Video to import updated.'))
119
120 this.router.navigate([ '/my-account', 'video-imports' ])
121 },
122
123 err => {
124 this.isUpdatingVideo = false
125 this.notificationsService.error(this.i18n('Error'), err.message)
126 console.error(err)
127 }
128 )
129
130 }
131
132 private hydrateFormFromVideo () {
133 this.form.patchValue(this.video.toFormPatch())
134 }
135 }