|
|
@ -1,5 +1,6 @@
|
|
|
|
import * as config from "config";
|
|
|
|
import * as config from "config";
|
|
|
|
import * as crypto from "crypto";
|
|
|
|
import * as crypto from "crypto";
|
|
|
|
|
|
|
|
import {FfprobeData} from "fluent-ffmpeg";
|
|
|
|
import * as ffmpeg from "fluent-ffmpeg";
|
|
|
|
import * as ffmpeg from "fluent-ffmpeg";
|
|
|
|
import * as fsx from "fs-extra";
|
|
|
|
import * as fsx from "fs-extra";
|
|
|
|
import * as path from "path";
|
|
|
|
import * as path from "path";
|
|
|
@ -103,33 +104,14 @@ export class UploadManager {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Converts a video into a smaller format and .mp4 and returns the web path
|
|
|
|
* Converts a video into a smaller format and .mp4 and returns the web path
|
|
|
|
* @param data
|
|
|
|
* @param data
|
|
|
|
* @param width
|
|
|
|
* @param extension
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public async processAndStoreVideo(data: Buffer, width: number = 720): Promise<string> {
|
|
|
|
public async processAndStoreVideo(data: Buffer, extension: string): Promise<string> {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
const fileBasename = UploadManager.getCrypticFileName() + extension;
|
|
|
|
try {
|
|
|
|
await fsx.ensureDir(this.dataDir);
|
|
|
|
const fileBasename = UploadManager.getCrypticFileName() + ".webm";
|
|
|
|
const filePath = path.join(this.dataDir, fileBasename);
|
|
|
|
await fsx.ensureDir(this.dataDir);
|
|
|
|
await fsx.writeFile(filePath, data);
|
|
|
|
const filePath = path.join(this.dataDir, fileBasename);
|
|
|
|
return `/${dataDirName}/${fileBasename}`;
|
|
|
|
const tempFile = filePath + ".tmp";
|
|
|
|
|
|
|
|
await fsx.writeFile(tempFile, data);
|
|
|
|
|
|
|
|
const video = ffmpeg(tempFile);
|
|
|
|
|
|
|
|
video
|
|
|
|
|
|
|
|
.size(`${width}x?`)
|
|
|
|
|
|
|
|
.toFormat("webm")
|
|
|
|
|
|
|
|
.on("end", async () => {
|
|
|
|
|
|
|
|
await fsx.unlink(tempFile);
|
|
|
|
|
|
|
|
resolve(`/${dataDirName}/${fileBasename}`);
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.on("error", async (err) => {
|
|
|
|
|
|
|
|
await fsx.unlink(tempFile);
|
|
|
|
|
|
|
|
reject(err);
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.save(filePath);
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
reject(err);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|