]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-add-components/video-import-url.component.ts
Support audio upload in client
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-add-components / video-import-url.component.ts
CommitLineData
fbad87b0
C
1import { Component, EventEmitter, OnInit, Output } from '@angular/core'
2import { Router } from '@angular/router'
78848714 3import { VideoPrivacy, VideoUpdate } from '../../../../../../shared/models/videos'
f8b2c1b4 4import { AuthService, Notifier, ServerService } from '../../../core'
78848714 5import { VideoService } from '../../../shared/video/video.service'
fbad87b0 6import { I18n } from '@ngx-translate/i18n-polyfill'
fbad87b0 7import { LoadingBarService } from '@ngx-loading-bar/core'
78848714
C
8import { VideoSend } from '@app/videos/+video-edit/video-add-components/video-send'
9import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
10import { VideoEdit } from '@app/shared/video/video-edit.model'
11import { FormValidatorService } from '@app/shared'
fbad87b0 12import { VideoCaptionService } from '@app/shared/video-caption'
78848714 13import { VideoImportService } from '@app/shared/video-import'
7373507f 14import { scrollToTop } from '@app/shared/misc/utils'
fbad87b0
C
15
16@Component({
047559af
C
17 selector: 'my-video-import-url',
18 templateUrl: './video-import-url.component.html',
fbad87b0 19 styleUrls: [
78848714 20 '../shared/video-edit.component.scss',
457bb213 21 './video-send.scss'
fbad87b0
C
22 ]
23})
047559af 24export class VideoImportUrlComponent extends VideoSend implements OnInit, CanComponentDeactivate {
fbad87b0 25 @Output() firstStepDone = new EventEmitter<string>()
7373507f 26 @Output() firstStepError = new EventEmitter<void>()
fbad87b0
C
27
28 targetUrl = ''
fbad87b0
C
29
30 isImportingVideo = false
31 hasImportedVideo = false
32 isUpdatingVideo = false
33
fbad87b0 34 video: VideoEdit
7373507f 35 error: string
fbad87b0 36
990b6a0b 37 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
43620009 38
fbad87b0
C
39 constructor (
40 protected formValidatorService: FormValidatorService,
43620009 41 protected loadingBar: LoadingBarService,
f8b2c1b4 42 protected notifier: Notifier,
43620009
C
43 protected authService: AuthService,
44 protected serverService: ServerService,
45 protected videoService: VideoService,
46 protected videoCaptionService: VideoCaptionService,
fbad87b0 47 private router: Router,
fbad87b0 48 private videoImportService: VideoImportService,
fbad87b0
C
49 private i18n: I18n
50 ) {
51 super()
52 }
53
54 ngOnInit () {
43620009 55 super.ngOnInit()
fbad87b0
C
56 }
57
58 canDeactivate () {
59 return { canDeactivate: true }
60 }
61
fbad87b0
C
62 isTargetUrlValid () {
63 return this.targetUrl && this.targetUrl.match(/https?:\/\//)
64 }
65
66 importVideo () {
67 this.isImportingVideo = true
68
69 const videoUpdate: VideoUpdate = {
70 privacy: this.firstStepPrivacyId,
71 waitTranscoding: false,
72 commentsEnabled: true,
7f2cfe3a 73 downloadEnabled: true,
fbad87b0
C
74 channelId: this.firstStepChannelId
75 }
76
5d08a6a7
C
77 this.loadingBar.start()
78
ce33919c 79 this.videoImportService.importVideoUrl(this.targetUrl, videoUpdate).subscribe(
fbad87b0 80 res => {
5d08a6a7 81 this.loadingBar.complete()
fbad87b0
C
82 this.firstStepDone.emit(res.video.name)
83 this.isImportingVideo = false
84 this.hasImportedVideo = true
85
86 this.video = new VideoEdit(Object.assign(res.video, {
87 commentsEnabled: videoUpdate.commentsEnabled,
7f2cfe3a 88 downloadEnabled: videoUpdate.downloadEnabled,
fbad87b0
C
89 support: null,
90 thumbnailUrl: null,
91 previewUrl: null
92 }))
8cd7faaa 93
71d00bfd 94 this.explainedVideoPrivacies = this.videoService.explainedPrivacyLabels(this.videoPrivacies)
8cd7faaa 95
fbad87b0
C
96 this.hydrateFormFromVideo()
97 },
98
99 err => {
5d08a6a7 100 this.loadingBar.complete()
fbad87b0 101 this.isImportingVideo = false
7373507f 102 this.firstStepError.emit()
f8b2c1b4 103 this.notifier.error(err.message)
fbad87b0
C
104 }
105 )
106 }
107
108 updateSecondStep () {
109 if (this.checkForm() === false) {
110 return
111 }
112
113 this.video.patch(this.form.value)
114
fbad87b0
C
115 this.isUpdatingVideo = true
116
117 // Update the video
43620009 118 this.updateVideoAndCaptions(this.video)
fbad87b0
C
119 .subscribe(
120 () => {
121 this.isUpdatingVideo = false
f8b2c1b4 122 this.notifier.success(this.i18n('Video to import updated.'))
fbad87b0 123
b2977eec 124 this.router.navigate([ '/my-account', 'video-imports' ])
fbad87b0
C
125 },
126
127 err => {
7373507f
C
128 this.error = err.message
129 scrollToTop()
fbad87b0
C
130 console.error(err)
131 }
132 )
133
134 }
135
136 private hydrateFormFromVideo () {
137 this.form.patchValue(this.video.toFormPatch())
138 }
139}