]> 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 'release/4.0.0' into develop
[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 { AfterViewInit, Component, EventEmitter, OnInit, Output } from '@angular/core'
3 import { Router } from '@angular/router'
4 import { AuthService, CanComponentDeactivate, HooksService, 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 { 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, AfterViewInit, 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 constructor (
35 protected formValidatorService: FormValidatorService,
36 protected loadingBar: LoadingBarService,
37 protected notifier: Notifier,
38 protected authService: AuthService,
39 protected serverService: ServerService,
40 protected videoService: VideoService,
41 protected videoCaptionService: VideoCaptionService,
42 private router: Router,
43 private videoImportService: VideoImportService,
44 private hooks: HooksService
45 ) {
46 super()
47 }
48
49 ngOnInit () {
50 super.ngOnInit()
51 }
52
53 ngAfterViewInit () {
54 this.hooks.runAction('action:video-url-import.init', 'video-edit')
55 }
56
57 canDeactivate () {
58 return { canDeactivate: true }
59 }
60
61 isTargetUrlValid () {
62 return this.targetUrl?.match(/https?:\/\//)
63 }
64
65 importVideo () {
66 this.isImportingVideo = true
67
68 const videoUpdate: VideoUpdate = {
69 privacy: this.highestPrivacy,
70 waitTranscoding: false,
71 channelId: this.firstStepChannelId
72 }
73
74 this.loadingBar.useRef().start()
75
76 this.videoImportService
77 .importVideoUrl(this.targetUrl, videoUpdate)
78 .pipe(
79 switchMap(res => {
80 return this.videoCaptionService
81 .listCaptions(res.video.id)
82 .pipe(
83 map(result => ({ video: res.video, videoCaptions: result.data }))
84 )
85 })
86 )
87 .subscribe({
88 next: ({ video, videoCaptions }) => {
89 this.loadingBar.useRef().complete()
90 this.firstStepDone.emit(video.name)
91 this.isImportingVideo = false
92 this.hasImportedVideo = true
93
94 const absoluteAPIUrl = getAbsoluteAPIUrl()
95
96 const thumbnailUrl = video.thumbnailPath
97 ? absoluteAPIUrl + video.thumbnailPath
98 : null
99
100 const previewUrl = video.previewPath
101 ? absoluteAPIUrl + video.previewPath
102 : null
103
104 this.video = new VideoEdit(Object.assign(video, {
105 commentsEnabled: videoUpdate.commentsEnabled,
106 downloadEnabled: videoUpdate.downloadEnabled,
107 privacy: { id: this.firstStepPrivacyId },
108 support: null,
109 thumbnailUrl,
110 previewUrl
111 }))
112
113 this.videoCaptions = videoCaptions
114
115 hydrateFormFromVideo(this.form, this.video, true)
116 },
117
118 error: err => {
119 this.loadingBar.useRef().complete()
120 this.isImportingVideo = false
121 this.firstStepError.emit()
122 this.notifier.error(err.message)
123 }
124 })
125 }
126
127 async updateSecondStep () {
128 if (!await this.isFormValid()) return
129
130 this.video.patch(this.form.value)
131
132 this.isUpdatingVideo = true
133
134 // Update the video
135 this.updateVideoAndCaptions(this.video)
136 .subscribe({
137 next: () => {
138 this.isUpdatingVideo = false
139 this.notifier.success($localize`Video to import updated.`)
140
141 this.router.navigate([ '/my-library', 'video-imports' ])
142 },
143
144 error: err => {
145 this.error = err.message
146 scrollToTop()
147 console.error(err)
148 }
149 })
150 }
151 }