From 83514973553638c905511aec80ce0bcd82e61f6f Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 23 Jan 2020 16:13:49 +0100 Subject: [PATCH 1/3] Fix aspect ratio of image changes when uploaded --- src/routes/UploadRoute.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/UploadRoute.ts b/src/routes/UploadRoute.ts index ff0911f..0354075 100644 --- a/src/routes/UploadRoute.ts +++ b/src/routes/UploadRoute.ts @@ -204,7 +204,7 @@ export class UploadRoute extends Route { const post = await Post.findByPk(postId); if (post.authorId === request.session.userId) { if (is.image(postMedia.mimetype)) { - fileName = await this.uploadManager.processAndStoreImage(postMedia.data, 1080, 720, "contain"); + fileName = await this.uploadManager.processAndStoreImage(postMedia.data, 1080, 720, "inside"); } else if (is.video(postMedia.mimetype)) { fileName = await this.uploadManager.processAndStoreVideo(postMedia.data, 1080); } else { From 0bae164b9b85ad8addc81b02c061acc60a12216d Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 23 Jan 2020 16:33:46 +0100 Subject: [PATCH 2/3] Remove ffmpeg processing - Remove ffmpeg video processing due to high cpu usages --- config/default.toml | 3 +++ src/lib/UploadManager.ts | 34 ++++++++-------------------------- src/lib/models/Post.ts | 3 ++- src/routes/UploadRoute.ts | 3 ++- 4 files changed, 15 insertions(+), 28 deletions(-) diff --git a/config/default.toml b/config/default.toml index d66d813..b804130 100644 --- a/config/default.toml +++ b/config/default.toml @@ -66,6 +66,9 @@ imageFormat = "png" # the max file size for uploading in bytes maxFileSize = 10_485_760 +# the max length of a video in seconds +maxVideoLenght = 60 + # Configuration for the api rate limit [api.rateLimit] diff --git a/src/lib/UploadManager.ts b/src/lib/UploadManager.ts index 9b7280c..916b8c2 100644 --- a/src/lib/UploadManager.ts +++ b/src/lib/UploadManager.ts @@ -1,5 +1,6 @@ import * as config from "config"; import * as crypto from "crypto"; +import {FfprobeData} from "fluent-ffmpeg"; import * as ffmpeg from "fluent-ffmpeg"; import * as fsx from "fs-extra"; 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 * @param data - * @param width + * @param extension */ - public async processAndStoreVideo(data: Buffer, width: number = 720): Promise { - return new Promise(async (resolve, reject) => { - try { - const fileBasename = UploadManager.getCrypticFileName() + ".webm"; - await fsx.ensureDir(this.dataDir); - const filePath = path.join(this.dataDir, 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); - } - }); + public async processAndStoreVideo(data: Buffer, extension: string): Promise { + const fileBasename = UploadManager.getCrypticFileName() + extension; + await fsx.ensureDir(this.dataDir); + const filePath = path.join(this.dataDir, fileBasename); + await fsx.writeFile(filePath, data); + return `/${dataDirName}/${fileBasename}`; } /** diff --git a/src/lib/models/Post.ts b/src/lib/models/Post.ts index 621f35c..dc0574e 100644 --- a/src/lib/models/Post.ts +++ b/src/lib/models/Post.ts @@ -1,3 +1,4 @@ +import * as config from "config"; import * as sqz from "sequelize"; import {BelongsTo, BelongsToMany, Column, CreatedAt, ForeignKey, Model, NotNull, Table} from "sequelize-typescript"; import markdown from "../markdown"; @@ -112,7 +113,7 @@ export class Post extends Model { public get media() { const url = this.getDataValue("mediaUrl"); if (url) { - const type = url.endsWith(".webm") ? "VIDEO" : "IMAGE"; + const type = url.endsWith(config.get("api.imageFormat")) ? "IMAGE" : "VIDEO"; return { type, url, diff --git a/src/routes/UploadRoute.ts b/src/routes/UploadRoute.ts index 0354075..114033e 100644 --- a/src/routes/UploadRoute.ts +++ b/src/routes/UploadRoute.ts @@ -206,7 +206,8 @@ export class UploadRoute extends Route { if (is.image(postMedia.mimetype)) { fileName = await this.uploadManager.processAndStoreImage(postMedia.data, 1080, 720, "inside"); } else if (is.video(postMedia.mimetype)) { - fileName = await this.uploadManager.processAndStoreVideo(postMedia.data, 1080); + fileName = await this.uploadManager.processAndStoreVideo(postMedia.data, postMedia.mimetype + .replace("video/", "")); } else { error = "Wrong type of file provided"; } From 78fce76407db9e9104f3c176a6cb62e2d94c8ff6 Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 23 Jan 2020 16:34:56 +0100 Subject: [PATCH 3/3] Remove ffmpeg dependencies --- package.json | 3 -- src/lib/UploadManager.ts | 5 --- yarn.lock | 70 +--------------------------------------- 3 files changed, 1 insertion(+), 77 deletions(-) diff --git a/package.json b/package.json index 7c101c0..9c1e164 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,6 @@ "@types/express-graphql": "^0.8.0", "@types/express-session": "^1.15.14", "@types/express-socket.io-session": "^1.3.2", - "@types/fluent-ffmpeg": "^2.1.13", "@types/fs-extra": "^8.0.0", "@types/graphql-query-complexity": "^0.2.1", "@types/http-status": "^0.2.30", @@ -61,7 +60,6 @@ "typescript": "^3.7.2" }, "dependencies": { - "@ffmpeg-installer/ffmpeg": "^1.0.20", "@types/body-parser": "^1.17.1", "@types/graphql-upload": "^8.0.3", "body-parser": "^1.19.0", @@ -76,7 +74,6 @@ "express-limiter": "^1.6.1", "express-session": "^1.16.2", "express-socket.io-session": "^1.3.5", - "fluent-ffmpeg": "^2.1.2", "fs-extra": "^8.1.0", "graphql": "^14.4.2", "graphql-import": "^0.7.1", diff --git a/src/lib/UploadManager.ts b/src/lib/UploadManager.ts index 916b8c2..fd1b7cd 100644 --- a/src/lib/UploadManager.ts +++ b/src/lib/UploadManager.ts @@ -1,17 +1,13 @@ import * as config from "config"; import * as crypto from "crypto"; -import {FfprobeData} from "fluent-ffmpeg"; -import * as ffmpeg from "fluent-ffmpeg"; import * as fsx from "fs-extra"; import * as path from "path"; import * as sharp from "sharp"; import {Readable} from "stream"; -import {ReadableStreamBuffer} from "stream-buffers"; import globals from "./globals"; const toArray = require("stream-to-array"); -const ffmpegPath = require("@ffmpeg-installer/ffmpeg").path; const dataDirName = "data"; interface IUploadConfirmation { @@ -51,7 +47,6 @@ export class UploadManager { constructor() { this.dataDir = path.join(globals.getPublicDir(), dataDirName); - ffmpeg.setFfmpegPath(ffmpegPath); } /** diff --git a/yarn.lock b/yarn.lock index 5be4d88..7336849 100644 --- a/yarn.lock +++ b/yarn.lock @@ -18,54 +18,6 @@ esutils "^2.0.2" js-tokens "^4.0.0" -"@ffmpeg-installer/darwin-x64@4.1.0": - version "4.1.0" - resolved "https://registry.yarnpkg.com/@ffmpeg-installer/darwin-x64/-/darwin-x64-4.1.0.tgz#48e1706c690e628148482bfb64acb67472089aaa" - integrity sha512-Z4EyG3cIFjdhlY8wI9aLUXuH8nVt7E9SlMVZtWvSPnm2sm37/yC2CwjUzyCQbJbySnef1tQwGG2Sx+uWhd9IAw== - -"@ffmpeg-installer/ffmpeg@^1.0.20": - version "1.0.20" - resolved "https://registry.yarnpkg.com/@ffmpeg-installer/ffmpeg/-/ffmpeg-1.0.20.tgz#d3c9c2bbcd76149468fb0886c2b3fe9e4795490b" - integrity sha512-wbgd//6OdwbFXYgV68ZyKrIcozEQpUKlvV66XHaqO2h3sFbX0jYLzx62Q0v8UcFWN21LoxT98NU2P+K0OWsKNA== - optionalDependencies: - "@ffmpeg-installer/darwin-x64" "4.1.0" - "@ffmpeg-installer/linux-arm" "4.1.3" - "@ffmpeg-installer/linux-arm64" "4.1.4" - "@ffmpeg-installer/linux-ia32" "4.1.0" - "@ffmpeg-installer/linux-x64" "4.1.0" - "@ffmpeg-installer/win32-ia32" "4.1.0" - "@ffmpeg-installer/win32-x64" "4.1.0" - -"@ffmpeg-installer/linux-arm64@4.1.4": - version "4.1.4" - resolved "https://registry.yarnpkg.com/@ffmpeg-installer/linux-arm64/-/linux-arm64-4.1.4.tgz#7219f3f901bb67f7926cb060b56b6974a6cad29f" - integrity sha512-dljEqAOD0oIM6O6DxBW9US/FkvqvQwgJ2lGHOwHDDwu/pX8+V0YsDL1xqHbj1DMX/+nP9rxw7G7gcUvGspSoKg== - -"@ffmpeg-installer/linux-arm@4.1.3": - version "4.1.3" - resolved "https://registry.yarnpkg.com/@ffmpeg-installer/linux-arm/-/linux-arm-4.1.3.tgz#c554f105ed5f10475ec25d7bec94926ce18db4c1" - integrity sha512-NDf5V6l8AfzZ8WzUGZ5mV8O/xMzRag2ETR6+TlGIsMHp81agx51cqpPItXPib/nAZYmo55Bl2L6/WOMI3A5YRg== - -"@ffmpeg-installer/linux-ia32@4.1.0": - version "4.1.0" - resolved "https://registry.yarnpkg.com/@ffmpeg-installer/linux-ia32/-/linux-ia32-4.1.0.tgz#adad70b0d0d9d8d813983d6e683c5a338a75e442" - integrity sha512-0LWyFQnPf+Ij9GQGD034hS6A90URNu9HCtQ5cTqo5MxOEc7Rd8gLXrJvn++UmxhU0J5RyRE9KRYstdCVUjkNOQ== - -"@ffmpeg-installer/linux-x64@4.1.0": - version "4.1.0" - resolved "https://registry.yarnpkg.com/@ffmpeg-installer/linux-x64/-/linux-x64-4.1.0.tgz#b4a5d89c4e12e6d9306dbcdc573df716ec1c4323" - integrity sha512-Y5BWhGLU/WpQjOArNIgXD3z5mxxdV8c41C+U15nsE5yF8tVcdCGet5zPs5Zy3Ta6bU7haGpIzryutqCGQA/W8A== - -"@ffmpeg-installer/win32-ia32@4.1.0": - version "4.1.0" - resolved "https://registry.yarnpkg.com/@ffmpeg-installer/win32-ia32/-/win32-ia32-4.1.0.tgz#6eac4fb691b64c02e7a116c1e2d167f3e9b40638" - integrity sha512-FV2D7RlaZv/lrtdhaQ4oETwoFUsUjlUiasiZLDxhEUPdNDWcH1OU9K1xTvqz+OXLdsmYelUDuBS/zkMOTtlUAw== - -"@ffmpeg-installer/win32-x64@4.1.0": - version "4.1.0" - resolved "https://registry.yarnpkg.com/@ffmpeg-installer/win32-x64/-/win32-x64-4.1.0.tgz#17e8699b5798d4c60e36e2d6326a8ebe5e95a2c5" - integrity sha512-Drt5u2vzDnIONf4ZEkKtFlbvwj6rI3kxw1Ck9fpudmtgaZIHD4ucsWB2lCZBXRxJgXR+2IMSti+4rtM4C4rXgg== - "@types/accepts@*": version "1.3.5" resolved "https://registry.yarnpkg.com/@types/accepts/-/accepts-1.3.5.tgz#c34bec115cfc746e04fe5a059df4ce7e7b391575" @@ -210,13 +162,6 @@ "@types/express-serve-static-core" "*" "@types/serve-static" "*" -"@types/fluent-ffmpeg@^2.1.13": - version "2.1.13" - resolved "https://registry.yarnpkg.com/@types/fluent-ffmpeg/-/fluent-ffmpeg-2.1.13.tgz#bfffbcf298b0980924e9ba9aa471aba234626afb" - integrity sha512-hg87ZQb9WVcNGQHNhrYwWJM0ARNYbQbLGh1c6CfPl55/I+BH5UTpFJAr5aZWYGbl8BFVY82oF5iG4I+Ra3btiQ== - dependencies: - "@types/node" "*" - "@types/fs-capacitor@*": version "2.0.0" resolved "https://registry.yarnpkg.com/@types/fs-capacitor/-/fs-capacitor-2.0.0.tgz#17113e25817f584f58100fb7a08eed288b81956e" @@ -734,11 +679,6 @@ async-settle@^1.0.0: dependencies: async-done "^1.2.2" -async@>=0.2.9: - version "3.1.0" - resolved "https://registry.yarnpkg.com/async/-/async-3.1.0.tgz#42b3b12ae1b74927b5217d8c0016baaf62463772" - integrity sha512-4vx/aaY6j/j3Lw3fbCHNWP0pPaTCew3F6F3hYyl/tHs/ndmV1q7NW9T5yuJ2XAGwdQrP+6Wu20x06U4APo/iQQ== - async@^2.6.1: version "2.6.3" resolved "https://registry.yarnpkg.com/async/-/async-2.6.3.tgz#d72625e2344a3656e3a3ad4fa749fa83299d82ff" @@ -2149,14 +2089,6 @@ flat@^4.1.0: dependencies: is-buffer "~2.0.3" -fluent-ffmpeg@^2.1.2: - version "2.1.2" - resolved "https://registry.yarnpkg.com/fluent-ffmpeg/-/fluent-ffmpeg-2.1.2.tgz#c952de2240f812ebda0aa8006d7776ee2acf7d74" - integrity sha1-yVLeIkD4EuvaCqgAbXd27irPfXQ= - dependencies: - async ">=0.2.9" - which "^1.1.1" - flush-write-stream@^1.0.2: version "1.1.1" resolved "https://registry.yarnpkg.com/flush-write-stream/-/flush-write-stream-1.1.1.tgz#8dd7d873a1babc207d94ead0c2e0e44276ebf2e8" @@ -5666,7 +5598,7 @@ which-pm-runs@^1.0.0: resolved "https://registry.yarnpkg.com/which-pm-runs/-/which-pm-runs-1.0.0.tgz#670b3afbc552e0b55df6b7780ca74615f23ad1cb" integrity sha1-Zws6+8VS4LVd9rd4DKdGFfI60cs= -which@1.3.1, which@^1.1.1, which@^1.2.14: +which@1.3.1, which@^1.2.14: version "1.3.1" resolved "https://registry.yarnpkg.com/which/-/which-1.3.1.tgz#a45043d54f5805316da8d62f9f50918d3da70b0a" integrity sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==