]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-edit/video-add-components/video-import-url.component.ts
c447c179d35a575cba30a3ed61092832e96b72c8
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / video-add-components / video-import-url.component.ts
1 import { map, switchMap } from 'rxjs/operators'
2 import { Component, EventEmitter, OnInit, Output } from '@angular/core'
3 import { Router } from '@angular/router'
4 import { AuthService, CanComponentDeactivate, Notifier, ServerService } from '@app/core'
5 import { getAbsoluteAPIUrl, scrollToTop } from '@app/helpers'
6 import { FormValidatorService } from '@app/shared/shared-forms'
7 import { VideoCaptionService, VideoEdit, VideoImportService, VideoService } from '@app/shared/shared-main'
8 import { LoadingBarService } from '@ngx-loading-bar/core'
9 import { VideoPrivacy, VideoUpdate } from '@shared/models'
10 import { hydrateFormFromVideo } from '../shared/video-edit-utils'
11 import { VideoSend } from './video-send'
12
13 @Component({
14 selector: 'my-video-import-url',
15 templateUrl: './video-import-url.component.html',
16 styleUrls: [
17 '../shared/video-edit.component.scss',
18 './video-send.scss'
19 ]
20 })
21 export class VideoImportUrlComponent extends VideoSend implements OnInit, CanComponentDeactivate {
22 @Output() firstStepDone = new EventEmitter<string>()
23 @Output() firstStepError = new EventEmitter<void>()
24
25 targetUrl = ''
26
27 isImportingVideo = false
28 hasImportedVideo = false
29 isUpdatingVideo = false
30
31 video: VideoEdit
32 error: string
33
34 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
35
36 constructor (
37 protected formValidatorService: FormValidatorService,
38 protected loadingBar: LoadingBarService,
39 protected notifier: Notifier,
40 protected authService: AuthService,
41 protected serverService: ServerService,
42 protected videoService: VideoService,
43 protected videoCaptionService: VideoCaptionService,
44 private router: Router,
45 private videoImportService: VideoImportService
46 ) {
47 super()
48 }
49
50 ngOnInit () {
51 super.ngOnInit()
52 }
53
54 canDeactivate () {
55 return { canDeactivate: true }
56 }
57
58 isTargetUrlValid () {
59 return this.targetUrl && this.targetUrl.match(/https?:\/\//)
60 }
61
62 importVideo () {
63 this.isImportingVideo = true
64
65 const videoUpdate: VideoUpdate = {
66 privacy: VideoPrivacy.PRIVATE,
67 waitTranscoding: false,
68 commentsEnabled: true,
69 downloadEnabled: true,
70 channelId: this.firstStepChannelId
71 }
72
73 this.loadingBar.useRef().start()
74
75 this.videoImportService
76 .importVideoUrl(this.targetUrl, videoUpdate)
77 .pipe(
78 switchMap(res => {
79 return this.videoCaptionService
80 .listCaptions(res.video.id)
81 .pipe(
82 map(result => ({ video: res.video, videoCaptions: result.data }))
83 )
84 })
85 )
86 .subscribe(
87 ({ video, videoCaptions }) => {
88 this.loadingBar.useRef().complete()
89 this.firstStepDone.emit(video.name)
90 this.isImportingVideo = false
91 this.hasImportedVideo = true
92
93 const absoluteAPIUrl = getAbsoluteAPIUrl()
94
95 const thumbnailUrl = video.thumbnailPath
96 ? absoluteAPIUrl + video.thumbnailPath
97 : null
98
99 const previewUrl = video.previewPath
100 ? absoluteAPIUrl + video.previewPath
101 : null
102
103 this.video = new VideoEdit(Object.assign(video, {
104 commentsEnabled: videoUpdate.commentsEnabled,
105 downloadEnabled: videoUpdate.downloadEnabled,
106 privacy: { id: this.firstStepPrivacyId },
107 support: null,
108 thumbnailUrl,
109 previewUrl
110 }))
111
112 this.videoCaptions = videoCaptions
113
114 hydrateFormFromVideo(this.form, this.video, true)
115 },
116
117 err => {
118 this.loadingBar.useRef().complete()
119 this.isImportingVideo = false
120 this.firstStepError.emit()
121 this.notifier.error(err.message)
122 }
123 )
124 }
125
126 updateSecondStep () {
127 if (this.checkForm() === false) {
128 return
129 }
130
131 this.video.patch(this.form.value)
132
133 this.isUpdatingVideo = true
134
135 // Update the video
136 this.updateVideoAndCaptions(this.video)
137 .subscribe(
138 () => {
139 this.isUpdatingVideo = false
140 this.notifier.success($localize`Video to import updated.`)
141
142 this.router.navigate([ '/my-library', 'video-imports' ])
143 },
144
145 err => {
146 this.error = err.message
147 scrollToTop()
148 console.error(err)
149 }
150 )
151 }
152 }