]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-edit/video-add-components/video-import-url.component.ts
Fix help component border
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-edit / video-add-components / video-import-url.component.ts
1 import { Component, EventEmitter, OnInit, Output } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { VideoPrivacy, VideoUpdate } from '../../../../../../shared/models/videos'
4 import { AuthService, Notifier, ServerService } from '../../../core'
5 import { VideoService } from '../../../shared/video/video.service'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { LoadingBarService } from '@ngx-loading-bar/core'
8 import { VideoSend } from '@app/videos/+video-edit/video-add-components/video-send'
9 import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
10 import { VideoEdit } from '@app/shared/video/video-edit.model'
11 import { FormValidatorService } from '@app/shared'
12 import { VideoCaptionService } from '@app/shared/video-caption'
13 import { VideoImportService } from '@app/shared/video-import'
14 import { scrollToTop } from '@app/shared/misc/utils'
15
16 @Component({
17 selector: 'my-video-import-url',
18 templateUrl: './video-import-url.component.html',
19 styleUrls: [
20 '../shared/video-edit.component.scss',
21 './video-send.scss'
22 ]
23 })
24 export class VideoImportUrlComponent extends VideoSend implements OnInit, CanComponentDeactivate {
25 @Output() firstStepDone = new EventEmitter<string>()
26 @Output() firstStepError = new EventEmitter<void>()
27
28 targetUrl = ''
29
30 isImportingVideo = false
31 hasImportedVideo = false
32 isUpdatingVideo = false
33
34 video: VideoEdit
35 error: string
36
37 protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC
38
39 constructor (
40 protected formValidatorService: FormValidatorService,
41 protected loadingBar: LoadingBarService,
42 protected notifier: Notifier,
43 protected authService: AuthService,
44 protected serverService: ServerService,
45 protected videoService: VideoService,
46 protected videoCaptionService: VideoCaptionService,
47 private router: Router,
48 private videoImportService: VideoImportService,
49 private i18n: I18n
50 ) {
51 super()
52 }
53
54 ngOnInit () {
55 super.ngOnInit()
56 }
57
58 canDeactivate () {
59 return { canDeactivate: true }
60 }
61
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,
73 downloadEnabled: true,
74 channelId: this.firstStepChannelId
75 }
76
77 this.loadingBar.start()
78
79 this.videoImportService.importVideoUrl(this.targetUrl, videoUpdate).subscribe(
80 res => {
81 this.loadingBar.complete()
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,
88 downloadEnabled: videoUpdate.downloadEnabled,
89 support: null,
90 thumbnailUrl: null,
91 previewUrl: null
92 }))
93
94 this.hydrateFormFromVideo()
95 },
96
97 err => {
98 this.loadingBar.complete()
99 this.isImportingVideo = false
100 this.firstStepError.emit()
101 this.notifier.error(err.message)
102 }
103 )
104 }
105
106 updateSecondStep () {
107 if (this.checkForm() === false) {
108 return
109 }
110
111 this.video.patch(this.form.value)
112
113 this.isUpdatingVideo = true
114
115 // Update the video
116 this.updateVideoAndCaptions(this.video)
117 .subscribe(
118 () => {
119 this.isUpdatingVideo = false
120 this.notifier.success(this.i18n('Video to import updated.'))
121
122 this.router.navigate([ '/my-account', 'video-imports' ])
123 },
124
125 err => {
126 this.error = err.message
127 scrollToTop()
128 console.error(err)
129 }
130 )
131
132 }
133
134 private hydrateFormFromVideo () {
135 this.form.patchValue(this.video.toFormPatch())
136 }
137 }