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