]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-edit/video-add-components/video-import-url.component.ts
Fix comments/download attributes on import
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / video-add-components / video-import-url.component.ts
1 import { forkJoin } from 'rxjs'
2 import { map, switchMap } from 'rxjs/operators'
3 import { AfterViewInit, Component, EventEmitter, OnInit, Output } from '@angular/core'
4 import { Router } from '@angular/router'
5 import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core'
6 import { scrollToTop } from '@app/helpers'
7 import { FormValidatorService } from '@app/shared/shared-forms'
8 import { VideoCaptionService, VideoEdit, VideoImportService, VideoService } from '@app/shared/shared-main'
9 import { LoadingBarService } from '@ngx-loading-bar/core'
10 import { VideoUpdate } from '@shared/models'
11 import { hydrateFormFromVideo } from '../shared/video-edit-utils'
12 import { VideoSend } from './video-send'
13
14 @Component({
15 selector: 'my-video-import-url',
16 templateUrl: './video-import-url.component.html',
17 styleUrls: [
18 '../shared/video-edit.component.scss',
19 './video-send.scss'
20 ]
21 })
22 export class VideoImportUrlComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
23 @Output() firstStepDone = new EventEmitter<string>()
24 @Output() firstStepError = new EventEmitter<void>()
25
26 targetUrl = ''
27
28 isImportingVideo = false
29 hasImportedVideo = false
30 isUpdatingVideo = false
31
32 video: VideoEdit
33 error: string
34
35 constructor (
36 protected formValidatorService: FormValidatorService,
37 protected loadingBar: LoadingBarService,
38 protected notifier: Notifier,
39 protected authService: AuthService,
40 protected serverService: ServerService,
41 protected videoService: VideoService,
42 protected videoCaptionService: VideoCaptionService,
43 private router: Router,
44 private videoImportService: VideoImportService,
45 private hooks: HooksService
46 ) {
47 super()
48 }
49
50 ngOnInit () {
51 super.ngOnInit()
52 }
53
54 ngAfterViewInit () {
55 this.hooks.runAction('action:video-url-import.init', 'video-edit')
56 }
57
58 canDeactivate () {
59 return { canDeactivate: true }
60 }
61
62 isTargetUrlValid () {
63 return this.targetUrl?.match(/https?:\/\//)
64 }
65
66 importVideo () {
67 this.isImportingVideo = true
68
69 const videoUpdate: VideoUpdate = {
70 privacy: this.highestPrivacy,
71 waitTranscoding: false,
72 channelId: this.firstStepChannelId
73 }
74
75 this.loadingBar.useRef().start()
76
77 this.videoImportService
78 .importVideoUrl(this.targetUrl, videoUpdate)
79 .pipe(
80 switchMap(previous => {
81 return forkJoin([
82 this.videoCaptionService.listCaptions(previous.video.uuid),
83 this.videoService.getVideo({ videoId: previous.video.uuid })
84 ]).pipe(map(([ videoCaptionsResult, video ]) => ({ videoCaptions: videoCaptionsResult.data, video })))
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 this.video = new VideoEdit(video)
95 this.video.patch({ privacy: this.firstStepPrivacyId })
96
97 this.videoCaptions = videoCaptions
98
99 hydrateFormFromVideo(this.form, this.video, true)
100 },
101
102 error: err => {
103 this.loadingBar.useRef().complete()
104 this.isImportingVideo = false
105 this.firstStepError.emit()
106 this.notifier.error(err.message)
107 }
108 })
109 }
110
111 async updateSecondStep () {
112 if (!await this.isFormValid()) return
113
114 this.video.patch(this.form.value)
115
116 this.isUpdatingVideo = true
117
118 // Update the video
119 this.updateVideoAndCaptions(this.video)
120 .subscribe({
121 next: () => {
122 this.isUpdatingVideo = false
123 this.notifier.success($localize`Video to import updated.`)
124
125 this.router.navigate([ '/my-library', 'video-imports' ])
126 },
127
128 error: err => {
129 this.error = err.message
130 scrollToTop()
131 console.error(err)
132 }
133 })
134 }
135 }