]>
Commit | Line | Data |
---|---|---|
c199c427 | 1 | import { Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core' |
ce33919c | 2 | import { Router } from '@angular/router' |
ce33919c | 3 | import { VideoPrivacy, VideoUpdate } from '../../../../../../shared/models/videos' |
f8b2c1b4 | 4 | import { AuthService, Notifier, ServerService } from '../../../core' |
ce33919c C |
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' | |
7373507f | 14 | import { scrollToTop } from '@app/shared/misc/utils' |
ce33919c C |
15 | |
16 | @Component({ | |
17 | selector: 'my-video-import-torrent', | |
18 | templateUrl: './video-import-torrent.component.html', | |
19 | styleUrls: [ | |
20 | '../shared/video-edit.component.scss', | |
457bb213 C |
21 | './video-import-torrent.component.scss', |
22 | './video-send.scss' | |
ce33919c C |
23 | ] |
24 | }) | |
25 | export class VideoImportTorrentComponent extends VideoSend implements OnInit, CanComponentDeactivate { | |
26 | @Output() firstStepDone = new EventEmitter<string>() | |
7373507f | 27 | @Output() firstStepError = new EventEmitter<void>() |
c199c427 | 28 | @ViewChild('torrentfileInput') torrentfileInput: ElementRef<HTMLInputElement> |
ce33919c | 29 | |
ce33919c C |
30 | magnetUri = '' |
31 | ||
32 | isImportingVideo = false | |
33 | hasImportedVideo = false | |
34 | isUpdatingVideo = false | |
35 | ||
36 | video: VideoEdit | |
7373507f | 37 | error: string |
ce33919c | 38 | |
990b6a0b | 39 | protected readonly DEFAULT_VIDEO_PRIVACY = VideoPrivacy.PUBLIC |
ce33919c C |
40 | |
41 | constructor ( | |
42 | protected formValidatorService: FormValidatorService, | |
43 | protected loadingBar: LoadingBarService, | |
f8b2c1b4 | 44 | protected notifier: Notifier, |
ce33919c C |
45 | protected authService: AuthService, |
46 | protected serverService: ServerService, | |
47 | protected videoService: VideoService, | |
48 | protected videoCaptionService: VideoCaptionService, | |
49 | private router: Router, | |
50 | private videoImportService: VideoImportService, | |
51 | private i18n: I18n | |
52 | ) { | |
53 | super() | |
54 | } | |
55 | ||
56 | ngOnInit () { | |
57 | super.ngOnInit() | |
58 | } | |
59 | ||
60 | canDeactivate () { | |
61 | return { canDeactivate: true } | |
62 | } | |
63 | ||
64 | isMagnetUrlValid () { | |
65 | return !!this.magnetUri | |
66 | } | |
67 | ||
990b6a0b | 68 | fileChange () { |
c199c427 | 69 | const torrentfile = this.torrentfileInput.nativeElement.files[0] |
990b6a0b C |
70 | if (!torrentfile) return |
71 | ||
72 | this.importVideo(torrentfile) | |
73 | } | |
74 | ||
75 | importVideo (torrentfile?: Blob) { | |
ce33919c C |
76 | this.isImportingVideo = true |
77 | ||
78 | const videoUpdate: VideoUpdate = { | |
79 | privacy: this.firstStepPrivacyId, | |
80 | waitTranscoding: false, | |
81 | commentsEnabled: true, | |
7f2cfe3a | 82 | downloadEnabled: true, |
ce33919c C |
83 | channelId: this.firstStepChannelId |
84 | } | |
85 | ||
86 | this.loadingBar.start() | |
87 | ||
990b6a0b | 88 | this.videoImportService.importVideoTorrent(torrentfile || this.magnetUri, videoUpdate).subscribe( |
ce33919c C |
89 | res => { |
90 | this.loadingBar.complete() | |
91 | this.firstStepDone.emit(res.video.name) | |
92 | this.isImportingVideo = false | |
93 | this.hasImportedVideo = true | |
94 | ||
95 | this.video = new VideoEdit(Object.assign(res.video, { | |
96 | commentsEnabled: videoUpdate.commentsEnabled, | |
7f2cfe3a | 97 | downloadEnabled: videoUpdate.downloadEnabled, |
ce33919c C |
98 | support: null, |
99 | thumbnailUrl: null, | |
100 | previewUrl: null | |
101 | })) | |
8cd7faaa | 102 | |
ce33919c C |
103 | this.hydrateFormFromVideo() |
104 | }, | |
105 | ||
106 | err => { | |
107 | this.loadingBar.complete() | |
108 | this.isImportingVideo = false | |
7373507f | 109 | this.firstStepError.emit() |
f8b2c1b4 | 110 | this.notifier.error(err.message) |
ce33919c C |
111 | } |
112 | ) | |
113 | } | |
114 | ||
115 | updateSecondStep () { | |
116 | if (this.checkForm() === false) { | |
117 | return | |
118 | } | |
119 | ||
120 | this.video.patch(this.form.value) | |
121 | ||
122 | this.isUpdatingVideo = true | |
123 | ||
124 | // Update the video | |
125 | this.updateVideoAndCaptions(this.video) | |
126 | .subscribe( | |
127 | () => { | |
128 | this.isUpdatingVideo = false | |
f8b2c1b4 | 129 | this.notifier.success(this.i18n('Video to import updated.')) |
ce33919c C |
130 | |
131 | this.router.navigate([ '/my-account', 'video-imports' ]) | |
132 | }, | |
133 | ||
134 | err => { | |
7373507f C |
135 | this.error = err.message |
136 | scrollToTop() | |
ce33919c C |
137 | console.error(err) |
138 | } | |
139 | ) | |
140 | ||
141 | } | |
142 | ||
143 | private hydrateFormFromVideo () { | |
144 | this.form.patchValue(this.video.toFormPatch()) | |
145 | } | |
146 | } |