From 03a92e9d8538c8ec47b0e4a4563429f9d1fd488f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Preu=C3=9F?= Date: Sat, 4 Mar 2023 00:07:51 +0100 Subject: [PATCH] Rewrite s3 uploader Update user on upload/boot --- electron/main/helpers.ts | 19 +++ electron/main/index.ts | 4 +- electron/rerun-manager/encoder.ts | 132 ++++++++++-------- electron/rerun-manager/file-uploader.ts | 39 ++++++ electron/rerun-manager/internal-server.ts | 22 +-- electron/rerun-manager/settings-repository.ts | 18 +++ 6 files changed, 152 insertions(+), 82 deletions(-) create mode 100644 electron/rerun-manager/file-uploader.ts diff --git a/electron/main/helpers.ts b/electron/main/helpers.ts index 6642894..2fedec9 100644 --- a/electron/main/helpers.ts +++ b/electron/main/helpers.ts @@ -1,8 +1,27 @@ import {BrowserWindow} from "electron"; +import {User} from "../../shared/schema"; +import axios from "axios"; export function emit(event: any, ...args: any) { // Send a message to all windows BrowserWindow.getAllWindows().forEach((win) => { win.webContents.send(event, ...args) }); +} + +export async function resolveUser(accessToken: string, tokenType: string): Promise { + const response = await axios.get('https://api.rerunmanager.com/v1/channels/me', { + headers: { + 'Accept': 'application/json', + 'Authorization': `${tokenType} ${accessToken}`, + } + }); + + return { + id: response.data.id, + name: response.data.name, + config: response.data.config, + avatar_url: response.data.avatar_url, + premium: response.data.premium, + } } \ No newline at end of file diff --git a/electron/main/index.ts b/electron/main/index.ts index 0e0b9f4..2cf9cbc 100644 --- a/electron/main/index.ts +++ b/electron/main/index.ts @@ -161,8 +161,8 @@ ipcMain.handle('encode', async (event: IpcMainInvokeEvent, ...args: any[]) => { onUploadProgress: (id, progress) => event.sender.send('encode-upload-progress', id, progress), onUploadComplete: (id, video) => event.sender.send('encode-upload-complete', id, video), onError: (id, error) => event.sender.send('encode-error', id, error), - }, settingsRepository.getSettings()); - return await encoder.encode() + }, settingsRepository); + return encoder.encode(); }) ipcMain.handle('commitSettings', async (event: IpcMainInvokeEvent, ...args: any[]) => settingsRepository.commitSettings(args[0])) settingsRepository.watch((settings: Settings) => emit('settings', settings)); diff --git a/electron/rerun-manager/encoder.ts b/electron/rerun-manager/encoder.ts index 377599d..b0b2353 100644 --- a/electron/rerun-manager/encoder.ts +++ b/electron/rerun-manager/encoder.ts @@ -1,7 +1,10 @@ - -import {EncoderListeners, EncoderOptions, Settings, User, Video} from "../../shared/schema"; +import {EncoderListeners, EncoderOptions, Video} from "../../shared/schema"; import * as fs from "fs"; import axios, {AxiosInstance} from "axios"; +import {SettingsRepository} from "./settings-repository"; +import * as path from "path"; +import {path as ffmpegPath} from "@ffmpeg-installer/ffmpeg"; +import {upload} from "./file-uploader"; export class Encoder { private readonly id: string; @@ -9,7 +12,7 @@ export class Encoder { private readonly output: string; private readonly options: EncoderOptions; private readonly listeners: EncoderListeners; - private readonly settings: Settings; + private readonly settingsRepository: SettingsRepository; private api: AxiosInstance; private s3: AxiosInstance; @@ -18,15 +21,17 @@ export class Encoder { input: string, options: EncoderOptions, listeners: EncoderListeners, - settings: Settings + settingsRepository: SettingsRepository ) { + this.id = id; this.input = input; - this.output = this.input.replace(/\.mp4$/, '.flv') + this.output = this.getOutputPath(input, 'flv'); this.options = options; this.listeners = listeners; - this.settings = settings; + this.settingsRepository = settingsRepository; + const settings = settingsRepository.getSettings(); this.api = axios.create({ baseURL: settings.endpoint, headers: { @@ -37,45 +42,36 @@ export class Encoder { this.s3 = axios.create({}); } - async encode(): Promise { + encode(): void { this.listeners.onStart(this.id) - const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path - console.log('ffmpegPath', ffmpegPath) - const ffmpeg = require('fluent-ffmpeg') - ffmpeg.setFfmpegPath(ffmpegPath) - let totalTime = 0; - ffmpeg(this.input) - .outputOptions(this.getOutputOptions()) - .output(this.output) - .on('start', () => { - console.log('start') - }) - .on('codecData', data => { - totalTime = parseInt(data.duration.replace(/:/g, '')) - }) - .on('progress', progress => { - const time = parseInt(progress.timemark.replace(/:/g, '')) - const percent = (time / totalTime) * 100 - console.log('progress', percent) - this.listeners.onProgress(this.id, percent) - }) - .on('end', async () => { - console.log('end') - // @ts-ignore - try { - const video = await this.requestUploadUrl(this.settings.credentials.user) - await this.upload(video) - } catch (error) { + if (this.requireEncoding()) { + const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path + console.log('ffmpegPath', ffmpegPath) + const ffmpeg = require('fluent-ffmpeg') + ffmpeg.setFfmpegPath(ffmpegPath) + let totalTime = 0; + + ffmpeg(this.input) + .output(this.output) + .outputOptions(this.getOutputOptions()) + .on('start', () => console.log('start')) + .on('codecData', data => totalTime = parseInt(data.duration.replace(/:/g, ''))) + .on('progress', progress => { + const time = parseInt(progress.timemark.replace(/:/g, '')) + const percent = (time / totalTime) * 100 + console.log('progress', percent) + this.listeners.onProgress(this.id, percent) + }) + .on('end', async () => this.requestUpload(this.output)) + .on('error', (error) => { console.log('error', error) this.listeners.onError(this.id, error.message) - } - }) - .on('error', (error) => { - console.log('error', error) - this.listeners.onError(this.id, error.message) - }) - .run() + }) + .run() + } else { + this.requestUpload(this.input) + } } private getOutputOptions() { @@ -94,13 +90,21 @@ export class Encoder { ] } - private async requestUploadUrl(user: User): Promise