]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-edit/video-add-components/video-import-url.component.ts
Don't break video scheduled publication
[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
fbad87b0
C
94 this.hydrateFormFromVideo()
95 },
96
97 err => {
5d08a6a7 98 this.loadingBar.complete()
fbad87b0 99 this.isImportingVideo = false
7373507f 100 this.firstStepError.emit()
f8b2c1b4 101 this.notifier.error(err.message)
fbad87b0
C
102 }
103 )
104 }
105
106 updateSecondStep () {
107 if (this.checkForm() === false) {
108 return
109 }
110
111 this.video.patch(this.form.value)
112
fbad87b0
C
113 this.isUpdatingVideo = true
114
115 // Update the video
43620009 116 this.updateVideoAndCaptions(this.video)
fbad87b0
C
117 .subscribe(
118 () => {
119 this.isUpdatingVideo = false
f8b2c1b4 120 this.notifier.success(this.i18n('Video to import updated.'))
fbad87b0 121
b2977eec 122 this.router.navigate([ '/my-account', 'video-imports' ])
fbad87b0
C
123 },
124
125 err => {
7373507f
C
126 this.error = err.message
127 scrollToTop()
fbad87b0
C
128 console.error(err)
129 }
130 )
131
132 }
133
134 private hydrateFormFromVideo () {
135 this.form.patchValue(this.video.toFormPatch())
136 }
137}