]> 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 { 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, getAbsoluteAPIUrl } from '@app/shared/misc/utils'
15 import { switchMap, map } from 'rxjs/operators'
16
17 @Component({
18 selector: 'my-video-import-url',
19 templateUrl: './video-import-url.component.html',
20 styleUrls: [
21 '../shared/video-edit.component.scss',
22 './video-send.scss'
23 ]
24 })
25 export class VideoImportUrlComponent extends VideoSend implements OnInit, CanComponentDeactivate {
26 @Output() firstStepDone = new EventEmitter<string>()
27 @Output() firstStepError = new EventEmitter<void>()
28
29 targetUrl = ''
30
31 isImportingVideo = false
32 hasImportedVideo = false
33 isUpdatingVideo = false
34
35 video: VideoEdit
36 error: string
37
38 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
39
40 constructor (
41 protected formValidatorService: FormValidatorService,
42 protected loadingBar: LoadingBarService,
43 protected notifier: Notifier,
44 protected authService: AuthService,
45 protected serverService: ServerService,
46 protected videoService: VideoService,
47 protected videoCaptionService: VideoCaptionService,
48 private router: Router,
49 private videoImportService: VideoImportService,
50 private i18n: I18n
51 ) {
52 super()
53 }
54
55 ngOnInit () {
56 super.ngOnInit()
57 }
58
59 canDeactivate () {
60 return { canDeactivate: true }
61 }
62
63 isTargetUrlValid () {
64 return this.targetUrl && this.targetUrl.match(/https?:\/\//)
65 }
66
67 importVideo () {
68 this.isImportingVideo = true
69
70 const videoUpdate: VideoUpdate = {
71 privacy: this.firstStepPrivacyId,
72 waitTranscoding: false,
73 commentsEnabled: true,
74 downloadEnabled: true,
75 channelId: this.firstStepChannelId
76 }
77
78 this.loadingBar.start()
79
80 this.videoImportService
81 .importVideoUrl(this.targetUrl, videoUpdate)
82 .pipe(
83 switchMap(res => {
84 return this.videoCaptionService
85 .listCaptions(res.video.id)
86 .pipe(
87 map(result => ({ video: res.video, videoCaptions: result.data }))
88 )
89 })
90 )
91 .subscribe(
92 ({ video, videoCaptions }) => {
93 this.loadingBar.complete()
94 this.firstStepDone.emit(video.name)
95 this.isImportingVideo = false
96 this.hasImportedVideo = true
97
98 const absoluteAPIUrl = getAbsoluteAPIUrl()
99
100 const thumbnailUrl = video.thumbnailPath
101 ? absoluteAPIUrl + video.thumbnailPath
102 : null
103
104 const previewUrl = video.previewPath
105 ? absoluteAPIUrl + video.previewPath
106 : null
107
108 this.video = new VideoEdit(Object.assign(video, {
109 commentsEnabled: videoUpdate.commentsEnabled,
110 downloadEnabled: videoUpdate.downloadEnabled,
111 support: null,
112 thumbnailUrl,
113 previewUrl
114 }))
115
116 this.videoCaptions = videoCaptions
117
118 this.hydrateFormFromVideo()
119 },
120
121 err => {
122 this.loadingBar.complete()
123 this.isImportingVideo = false
124 this.firstStepError.emit()
125 this.notifier.error(err.message)
126 }
127 )
128 }
129
130 updateSecondStep () {
131 if (this.checkForm() === false) {
132 return
133 }
134
135 this.video.patch(this.form.value)
136
137 this.isUpdatingVideo = true
138
139 // Update the video
140 this.updateVideoAndCaptions(this.video)
141 .subscribe(
142 () => {
143 this.isUpdatingVideo = false
144 this.notifier.success(this.i18n('Video to import updated.'))
145
146 this.router.navigate([ '/my-account', 'video-imports' ])
147 },
148
149 err => {
150 this.error = err.message
151 scrollToTop()
152 console.error(err)
153 }
154 )
155
156 }
157
158 private hydrateFormFromVideo () {
159 this.form.patchValue(this.video.toFormPatch())
160
161 const objects = [
162 {
163 url: 'thumbnailUrl',
164 name: 'thumbnailfile'
165 },
166 {
167 url: 'previewUrl',
168 name: 'previewfile'
169 }
170 ]
171
172 for (const obj of objects) {
173 fetch(this.video[obj.url])
174 .then(response => response.blob())
175 .then(data => {
176 this.form.patchValue({
177 [ obj.name ]: data
178 })
179 })
180 }
181 }
182 }