Skip to content

Commit

Permalink
Merge pull request #139 from harena-lab/development
Browse files Browse the repository at this point in the history
feat (artifacts/upload): conversion using handbrake
  • Loading branch information
santanche authored Apr 4, 2022
2 parents b41cfaa + 53d92c5 commit 5dadd30
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ FROM node:10

RUN apt update
RUN apt install vim -y
RUN apt install -qq handbrake-cli

WORKDIR /app

Expand All @@ -12,6 +13,8 @@ COPY ./src/adonisjs .
RUN npm i npm
RUN npm i -g @adonisjs/cli

RUN npm i [email protected] --save

RUN npm install

#RUN chown node:node /app
Expand Down
44 changes: 40 additions & 4 deletions src/adonisjs/app/Controllers/Http/v1/ArtifactController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
}
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -128,8 +165,7 @@ class ArtifactController {
}
}


// Missing check permission
// Missing check permission
async destroy ({ params, response }) {
const trx = await Database.beginTransaction()

Expand Down
1 change: 1 addition & 0 deletions src/adonisjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 5dadd30

Please sign in to comment.