]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-edit/video-add-components/video-import-url.component.ts
Enhance plugin video fields
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-edit / video-add-components / video-import-url.component.ts
CommitLineData
67ed6552 1import { map, switchMap } from 'rxjs/operators'
2e257e36 2import { AfterViewInit, Component, EventEmitter, OnInit, Output } from '@angular/core'
fbad87b0 3import { Router } from '@angular/router'
2e257e36 4import { AuthService, CanComponentDeactivate, HooksService, Notifier, ServerService } from '@app/core'
67ed6552
C
5import { getAbsoluteAPIUrl, scrollToTop } from '@app/helpers'
6import { FormValidatorService } from '@app/shared/shared-forms'
7import { VideoCaptionService, VideoEdit, VideoImportService, VideoService } from '@app/shared/shared-main'
67ed6552 8import { LoadingBarService } from '@ngx-loading-bar/core'
1378c0d3 9import { VideoUpdate } from '@shared/models'
c6c0fa6c 10import { hydrateFormFromVideo } from '../shared/video-edit-utils'
66357162 11import { VideoSend } from './video-send'
fbad87b0
C
12
13@Component({
047559af
C
14 selector: 'my-video-import-url',
15 templateUrl: './video-import-url.component.html',
fbad87b0 16 styleUrls: [
78848714 17 '../shared/video-edit.component.scss',
457bb213 18 './video-send.scss'
fbad87b0
C
19 ]
20})
2e257e36 21export class VideoImportUrlComponent extends VideoSend implements OnInit, AfterViewInit, CanComponentDeactivate {
fbad87b0 22 @Output() firstStepDone = new EventEmitter<string>()
7373507f 23 @Output() firstStepError = new EventEmitter<void>()
fbad87b0
C
24
25 targetUrl = ''
fbad87b0
C
26
27 isImportingVideo = false
28 hasImportedVideo = false
29 isUpdatingVideo = false
30
fbad87b0 31 video: VideoEdit
7373507f 32 error: string
fbad87b0
C
33
34 constructor (
35 protected formValidatorService: FormValidatorService,
43620009 36 protected loadingBar: LoadingBarService,
f8b2c1b4 37 protected notifier: Notifier,
43620009
C
38 protected authService: AuthService,
39 protected serverService: ServerService,
40 protected videoService: VideoService,
41 protected videoCaptionService: VideoCaptionService,
fbad87b0 42 private router: Router,
2e257e36
C
43 private videoImportService: VideoImportService,
44 private hooks: HooksService
45 ) {
fbad87b0
C
46 super()
47 }
48
49 ngOnInit () {
43620009 50 super.ngOnInit()
fbad87b0
C
51 }
52
2e257e36
C
53 ngAfterViewInit () {
54 this.hooks.runAction('action:video-url-import.init', 'video-edit')
55 }
56
fbad87b0
C
57 canDeactivate () {
58 return { canDeactivate: true }
59 }
60
fbad87b0 61 isTargetUrlValid () {
9df52d66 62 return this.targetUrl?.match(/https?:\/\//)
fbad87b0
C
63 }
64
65 importVideo () {
66 this.isImportingVideo = true
67
68 const videoUpdate: VideoUpdate = {
a3f45a2a 69 privacy: this.highestPrivacy,
fbad87b0 70 waitTranscoding: false,
fbad87b0
C
71 channelId: this.firstStepChannelId
72 }
73
a02b93ce 74 this.loadingBar.useRef().start()
5d08a6a7 75
50ad0a1c 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 )
1378c0d3
C
87 .subscribe({
88 next: ({ video, videoCaptions }) => {
a02b93ce 89 this.loadingBar.useRef().complete()
50ad0a1c 90 this.firstStepDone.emit(video.name)
91 this.isImportingVideo = false
92 this.hasImportedVideo = true
93
b1770a0a
K
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
50ad0a1c 104 this.video = new VideoEdit(Object.assign(video, {
105 commentsEnabled: videoUpdate.commentsEnabled,
106 downloadEnabled: videoUpdate.downloadEnabled,
d487a997 107 privacy: { id: this.firstStepPrivacyId },
50ad0a1c 108 support: null,
b1770a0a
K
109 thumbnailUrl,
110 previewUrl
50ad0a1c 111 }))
112
113 this.videoCaptions = videoCaptions
114
c6c0fa6c 115 hydrateFormFromVideo(this.form, this.video, true)
50ad0a1c 116 },
117
1378c0d3 118 error: err => {
a02b93ce 119 this.loadingBar.useRef().complete()
50ad0a1c 120 this.isImportingVideo = false
121 this.firstStepError.emit()
122 this.notifier.error(err.message)
123 }
1378c0d3 124 })
fbad87b0
C
125 }
126
127 updateSecondStep () {
128 if (this.checkForm() === false) {
129 return
130 }
131
132 this.video.patch(this.form.value)
133
fbad87b0
C
134 this.isUpdatingVideo = true
135
136 // Update the video
43620009 137 this.updateVideoAndCaptions(this.video)
1378c0d3
C
138 .subscribe({
139 next: () => {
fbad87b0 140 this.isUpdatingVideo = false
66357162 141 this.notifier.success($localize`Video to import updated.`)
fbad87b0 142
17119e4a 143 this.router.navigate([ '/my-library', 'video-imports' ])
fbad87b0
C
144 },
145
1378c0d3 146 error: err => {
7373507f
C
147 this.error = err.message
148 scrollToTop()
fbad87b0
C
149 console.error(err)
150 }
1378c0d3 151 })
fbad87b0
C
152 }
153}