]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-edit/video-add-components/video-import-url.component.ts
fix CONTRIBUTING.md command order (#3305)
[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'
fbad87b0
C
2import { Component, EventEmitter, OnInit, Output } from '@angular/core'
3import { Router } from '@angular/router'
67ed6552
C
4import { AuthService, CanComponentDeactivate, Notifier, ServerService } from '@app/core'
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'
67ed6552 9import { VideoPrivacy, 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})
047559af 21export class VideoImportUrlComponent extends VideoSend implements OnInit, 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 33
990b6a0b 34 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
43620009 35
fbad87b0
C
36 constructor (
37 protected formValidatorService: FormValidatorService,
43620009 38 protected loadingBar: LoadingBarService,
f8b2c1b4 39 protected notifier: Notifier,
43620009
C
40 protected authService: AuthService,
41 protected serverService: ServerService,
42 protected videoService: VideoService,
43 protected videoCaptionService: VideoCaptionService,
fbad87b0 44 private router: Router,
66357162
C
45 private videoImportService: VideoImportService
46 ) {
fbad87b0
C
47 super()
48 }
49
50 ngOnInit () {
43620009 51 super.ngOnInit()
fbad87b0
C
52 }
53
54 canDeactivate () {
55 return { canDeactivate: true }
56 }
57
fbad87b0
C
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: this.firstStepPrivacyId,
67 waitTranscoding: false,
68 commentsEnabled: true,
7f2cfe3a 69 downloadEnabled: true,
fbad87b0
C
70 channelId: this.firstStepChannelId
71 }
72
a02b93ce 73 this.loadingBar.useRef().start()
5d08a6a7 74
50ad0a1c 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 }) => {
a02b93ce 88 this.loadingBar.useRef().complete()
50ad0a1c 89 this.firstStepDone.emit(video.name)
90 this.isImportingVideo = false
91 this.hasImportedVideo = true
92
b1770a0a
K
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
50ad0a1c 103 this.video = new VideoEdit(Object.assign(video, {
104 commentsEnabled: videoUpdate.commentsEnabled,
105 downloadEnabled: videoUpdate.downloadEnabled,
106 support: null,
b1770a0a
K
107 thumbnailUrl,
108 previewUrl
50ad0a1c 109 }))
110
111 this.videoCaptions = videoCaptions
112
c6c0fa6c 113 hydrateFormFromVideo(this.form, this.video, true)
50ad0a1c 114 },
115
116 err => {
a02b93ce 117 this.loadingBar.useRef().complete()
50ad0a1c 118 this.isImportingVideo = false
119 this.firstStepError.emit()
120 this.notifier.error(err.message)
121 }
122 )
fbad87b0
C
123 }
124
125 updateSecondStep () {
126 if (this.checkForm() === false) {
127 return
128 }
129
130 this.video.patch(this.form.value)
131
fbad87b0
C
132 this.isUpdatingVideo = true
133
134 // Update the video
43620009 135 this.updateVideoAndCaptions(this.video)
fbad87b0
C
136 .subscribe(
137 () => {
138 this.isUpdatingVideo = false
66357162 139 this.notifier.success($localize`Video to import updated.`)
fbad87b0 140
b2977eec 141 this.router.navigate([ '/my-account', 'video-imports' ])
fbad87b0
C
142 },
143
144 err => {
7373507f
C
145 this.error = err.message
146 scrollToTop()
fbad87b0
C
147 console.error(err)
148 }
149 )
fbad87b0
C
150 }
151}