From 53d92c5f894b4e5b5c6a9cd01b27063d3966ef93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Santanch=C3=A8?= Date: Sun, 3 Apr 2022 21:05:21 -0300 Subject: [PATCH] feat (artifacts/upload): conversion using handbrake --- Dockerfile | 3 ++ .../Controllers/Http/v1/ArtifactController.js | 44 +++++++++++++++++-- src/adonisjs/package.json | 1 + 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index eb04b82..61a703c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,7 @@ FROM node:10 RUN apt update RUN apt install vim -y +RUN apt install -qq handbrake-cli WORKDIR /app @@ -12,6 +13,8 @@ COPY ./src/adonisjs . RUN npm i npm RUN npm i -g @adonisjs/cli +RUN npm i handbrake-js@5.0.2 --save + RUN npm install #RUN chown node:node /app diff --git a/src/adonisjs/app/Controllers/Http/v1/ArtifactController.js b/src/adonisjs/app/Controllers/Http/v1/ArtifactController.js index 977ed27..034682a 100644 --- a/src/adonisjs/app/Controllers/Http/v1/ArtifactController.js +++ b/src/adonisjs/app/Controllers/Http/v1/ArtifactController.js @@ -14,6 +14,8 @@ const Case = use('App/Models/v1/Case') const Quest = use('App/Models/v1/Quest') const CaseArtifacts = use('App/Models/CaseArtifact') +const hbjs = require('handbrake-js') + class ArtifactController { constructor () { // See this for more on MIM types: https://docs.openx.com/Content/publishers/adunit_linearvideo_mime_types.html @@ -27,6 +29,8 @@ class ArtifactController { this.validExtensions = ['png', 'jpg', 'jpeg', 'gif', 'svg', 'mp4', 'avi', 'wmv', 'mov'] + this.convertExtensions = ['avi', 'wmv', 'mov'] + this.relativePath = '/resources/artifacts/' this.baseUrl = Env.getOrFail('APP_URL') } @@ -52,8 +56,11 @@ class ArtifactController { const caseId = request.input('caseId', null) const artifactId = request.input('id') || await uuid4() - const artifactFileName = artifactId + '.' + extension + const artifactOriginalName = artifactId + '.' + extension + const artifactFileName = (this.convertExtensions.includes(extension)) + ? artifactId + '.mp4' : artifactId + '.' + extension let fs_path = Helpers.publicPath(this.relativePath) + const conv_path = fs_path + 'convert/' const artifact = new Artifact() artifact.id = artifactId @@ -109,7 +116,37 @@ class ArtifactController { bodyMessage.url = this.baseUrl + artifact.relative_path } - await file.move(fs_path, { name: artifactFileName, overwrite: false }) + if (this.convertExtensions.includes(extension)) { + await file.move(conv_path, { name: artifactOriginalName, + overwrite: false }) + console.log('=== converting ' + artifactOriginalName + ' to ' + + artifactFileName) + + const options = { + input: conv_path + artifactOriginalName, + output: conv_path + artifactFileName, + preset: 'Normal', + encoder: 'x264' + } + hbjs.spawn(options) + .on('error', err => { + console.log(err) + }) + .on('progress', progress => { + console.log( + 'Percent complete: %s, ETA: %s', + progress.percentComplete, + progress.eta + ) + }) + .on('complete', complete => { + Drive.move(conv_path + artifactFileName, + fs_path + artifactFileName) + Drive.delete(conv_path + artifactOriginalName) + }) + } else + await file.move(fs_path, { name: artifactFileName, overwrite: false }) + await auth.user.artifacts().save(artifact) return response.json(bodyMessage) @@ -128,8 +165,7 @@ class ArtifactController { } } - -// Missing check permission + // Missing check permission async destroy ({ params, response }) { const trx = await Database.beginTransaction() diff --git a/src/adonisjs/package.json b/src/adonisjs/package.json index 9f5cde1..d20a0e6 100644 --- a/src/adonisjs/package.json +++ b/src/adonisjs/package.json @@ -31,6 +31,7 @@ "cloudinary": "^1.14.0", "dateformat": "^3.0.3", "fs-extra": "^7.0.1", + "handbrake-js": "^5.0.2", "mysql": "^2.16.0", "npm": "^6.13.4", "uuid": "^3.3.2"