Merge branch 'develop' of Software_Engineering_I/greenvironment-server into master

pull/5/head
Trivernis 5 years ago committed by Gitea
commit 49a2ac151d

@ -66,6 +66,9 @@ imageFormat = "png"
# the max file size for uploading in bytes # the max file size for uploading in bytes
maxFileSize = 10_485_760 maxFileSize = 10_485_760
# the max length of a video in seconds
maxVideoLenght = 60
# Configuration for the api rate limit # Configuration for the api rate limit
[api.rateLimit] [api.rateLimit]

@ -32,7 +32,6 @@
"@types/express-graphql": "^0.8.0", "@types/express-graphql": "^0.8.0",
"@types/express-session": "^1.15.14", "@types/express-session": "^1.15.14",
"@types/express-socket.io-session": "^1.3.2", "@types/express-socket.io-session": "^1.3.2",
"@types/fluent-ffmpeg": "^2.1.13",
"@types/fs-extra": "^8.0.0", "@types/fs-extra": "^8.0.0",
"@types/graphql-query-complexity": "^0.2.1", "@types/graphql-query-complexity": "^0.2.1",
"@types/http-status": "^0.2.30", "@types/http-status": "^0.2.30",
@ -61,7 +60,6 @@
"typescript": "^3.7.2" "typescript": "^3.7.2"
}, },
"dependencies": { "dependencies": {
"@ffmpeg-installer/ffmpeg": "^1.0.20",
"@types/body-parser": "^1.17.1", "@types/body-parser": "^1.17.1",
"@types/graphql-upload": "^8.0.3", "@types/graphql-upload": "^8.0.3",
"body-parser": "^1.19.0", "body-parser": "^1.19.0",
@ -76,7 +74,6 @@
"express-limiter": "^1.6.1", "express-limiter": "^1.6.1",
"express-session": "^1.16.2", "express-session": "^1.16.2",
"express-socket.io-session": "^1.3.5", "express-socket.io-session": "^1.3.5",
"fluent-ffmpeg": "^2.1.2",
"fs-extra": "^8.1.0", "fs-extra": "^8.1.0",
"graphql": "^14.4.2", "graphql": "^14.4.2",
"graphql-import": "^0.7.1", "graphql-import": "^0.7.1",

@ -1,16 +1,13 @@
import * as config from "config"; import * as config from "config";
import * as crypto from "crypto"; import * as crypto from "crypto";
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";
import * as sharp from "sharp"; import * as sharp from "sharp";
import {Readable} from "stream"; import {Readable} from "stream";
import {ReadableStreamBuffer} from "stream-buffers";
import globals from "./globals"; import globals from "./globals";
const toArray = require("stream-to-array"); const toArray = require("stream-to-array");
const ffmpegPath = require("@ffmpeg-installer/ffmpeg").path;
const dataDirName = "data"; const dataDirName = "data";
interface IUploadConfirmation { interface IUploadConfirmation {
@ -50,7 +47,6 @@ export class UploadManager {
constructor() { constructor() {
this.dataDir = path.join(globals.getPublicDir(), dataDirName); this.dataDir = path.join(globals.getPublicDir(), dataDirName);
ffmpeg.setFfmpegPath(ffmpegPath);
} }
/** /**
@ -103,33 +99,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 {
const fileBasename = UploadManager.getCrypticFileName() + ".webm";
await fsx.ensureDir(this.dataDir); await fsx.ensureDir(this.dataDir);
const filePath = path.join(this.dataDir, fileBasename); const filePath = path.join(this.dataDir, fileBasename);
const tempFile = filePath + ".tmp"; await fsx.writeFile(filePath, data);
await fsx.writeFile(tempFile, data); return `/${dataDirName}/${fileBasename}`;
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);
}
});
} }
/** /**

@ -1,3 +1,4 @@
import * as config from "config";
import * as sqz from "sequelize"; import * as sqz from "sequelize";
import {BelongsTo, BelongsToMany, Column, CreatedAt, ForeignKey, Model, NotNull, Table} from "sequelize-typescript"; import {BelongsTo, BelongsToMany, Column, CreatedAt, ForeignKey, Model, NotNull, Table} from "sequelize-typescript";
import markdown from "../markdown"; import markdown from "../markdown";
@ -112,7 +113,7 @@ export class Post extends Model<Post> {
public get media() { public get media() {
const url = this.getDataValue("mediaUrl"); const url = this.getDataValue("mediaUrl");
if (url) { if (url) {
const type = url.endsWith(".webm") ? "VIDEO" : "IMAGE"; const type = url.endsWith(config.get("api.imageFormat")) ? "IMAGE" : "VIDEO";
return { return {
type, type,
url, url,

@ -204,9 +204,10 @@ export class UploadRoute extends Route {
const post = await Post.findByPk(postId); const post = await Post.findByPk(postId);
if (post.authorId === request.session.userId) { if (post.authorId === request.session.userId) {
if (is.image(postMedia.mimetype)) { 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)) { } 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 { } else {
error = "Wrong type of file provided"; error = "Wrong type of file provided";
} }

@ -18,54 +18,6 @@
esutils "^2.0.2" esutils "^2.0.2"
js-tokens "^4.0.0" 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@*": "@types/accepts@*":
version "1.3.5" version "1.3.5"
resolved "https://registry.yarnpkg.com/@types/accepts/-/accepts-1.3.5.tgz#c34bec115cfc746e04fe5a059df4ce7e7b391575" resolved "https://registry.yarnpkg.com/@types/accepts/-/accepts-1.3.5.tgz#c34bec115cfc746e04fe5a059df4ce7e7b391575"
@ -210,13 +162,6 @@
"@types/express-serve-static-core" "*" "@types/express-serve-static-core" "*"
"@types/serve-static" "*" "@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@*": "@types/fs-capacitor@*":
version "2.0.0" version "2.0.0"
resolved "https://registry.yarnpkg.com/@types/fs-capacitor/-/fs-capacitor-2.0.0.tgz#17113e25817f584f58100fb7a08eed288b81956e" 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: dependencies:
async-done "^1.2.2" 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: async@^2.6.1:
version "2.6.3" version "2.6.3"
resolved "https://registry.yarnpkg.com/async/-/async-2.6.3.tgz#d72625e2344a3656e3a3ad4fa749fa83299d82ff" resolved "https://registry.yarnpkg.com/async/-/async-2.6.3.tgz#d72625e2344a3656e3a3ad4fa749fa83299d82ff"
@ -2149,14 +2089,6 @@ flat@^4.1.0:
dependencies: dependencies:
is-buffer "~2.0.3" 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: flush-write-stream@^1.0.2:
version "1.1.1" version "1.1.1"
resolved "https://registry.yarnpkg.com/flush-write-stream/-/flush-write-stream-1.1.1.tgz#8dd7d873a1babc207d94ead0c2e0e44276ebf2e8" 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" resolved "https://registry.yarnpkg.com/which-pm-runs/-/which-pm-runs-1.0.0.tgz#670b3afbc552e0b55df6b7780ca74615f23ad1cb"
integrity sha1-Zws6+8VS4LVd9rd4DKdGFfI60cs= 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" version "1.3.1"
resolved "https://registry.yarnpkg.com/which/-/which-1.3.1.tgz#a45043d54f5805316da8d62f9f50918d3da70b0a" resolved "https://registry.yarnpkg.com/which/-/which-1.3.1.tgz#a45043d54f5805316da8d62f9f50918d3da70b0a"
integrity sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ== integrity sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==

Loading…
Cancel
Save