Skip to content

Commit

Permalink
serve static files and more stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
juandjara committed May 12, 2020
1 parent d0fd99d commit 4c729fd
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 9 deletions.
8 changes: 5 additions & 3 deletions api/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fs = require('fs')
const axios = require('axios')
const tempy = require('tempy')
const ffmpeg = require('fluent-ffmpeg')
const { getVideoName } = require('./storage')
const { getVideoName, STORAGE_PATH } = require('./storage')

function parseDuration (timeStr) {
const [h, m, s] = timeStr.split(':').map(Number)
Expand Down Expand Up @@ -31,6 +31,7 @@ async function processVideo (job, done) {

function ffmpegCommand (tempPath, job, done) {
let duration = 0
const outputFile = getVideoName(job)

async function deleteTempFile () {
await fs.promises.unlink(tempPath)
Expand Down Expand Up @@ -77,9 +78,10 @@ function ffmpegCommand (tempPath, job, done) {
.on('end', async () => {
await deleteTempFile()
job.progress(100)
done(null, getVideoName(job))
job.log(`[converter.js] Finished transcoding job for output file ${outputFile}`)
done(null, outputFile.replace(`${STORAGE_PATH}/`, ''))
})
.saveToFile(getVideoName(job))
.saveToFile(outputFile)
}

module.exports = { processVideo, ffmpegCommand }
6 changes: 5 additions & 1 deletion api/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require('dotenv').config()
const app = require('express')()
const express = require('express')
const app = express()
const cors = require('cors')
const helmet = require('helmet')
const logger = require('morgan')
Expand All @@ -10,6 +11,7 @@ const Queue = require('bull')
const redisMetrics = require('./redisMetrics')
const { processVideo } = require('./converter')
const { deleteVideo } = require('./storage')
const serveIndex = require('serve-index')

app.set('json spaces', 2)
app.use(cors())
Expand All @@ -25,6 +27,8 @@ const videoQueue = new Queue('video transcoding', {
})
videoQueue.process(processVideo)

app.use('/files', express.static(`${__dirname}/files`), serveIndex(`${__dirname}/files`, { icons: true }))

app.get('/', (req, res) => {
res.json({
name: pkg.name,
Expand Down
37 changes: 37 additions & 0 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"morgan": "^1.10.0",
"node-ffprobe": "^3.0.0",
"redis-info": "^3.0.7",
"serve-index": "^1.9.1",
"tempy": "^0.5.0"
},
"devDependencies": {
Expand Down
9 changes: 6 additions & 3 deletions api/storage.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
const path = require('path')
const fs = require('fs').promises

const STORAGE_PATH = './files'

function getVideoName (job, extension = '.webm') {
const name = path.basename(job.data.url)
const urlFilename = path.basename(job.data.url)
const ext = path.extname(job.data.url)
return decodeURIComponent(name.replace(ext, extension))
const outputFilename = decodeURIComponent(urlFilename.replace(ext, '')) + extension
return path.join(STORAGE_PATH, outputFilename)
}

function deleteVideo (job) {
const file = getVideoName(job)
return fs.unlink(file)
}

module.exports = { getVideoName, deleteVideo }
module.exports = { STORAGE_PATH, getVideoName, deleteVideo }
9 changes: 7 additions & 2 deletions www/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Button from './Button'
import AppStyles from './AppStyles'
import RedisStats from './RedisStats'
import Modal from './Modal'
import { getJobs, deleteJob, cancelJob, addJob, getLogs, getMetrics } from './apiService'
import { getJobs, deleteJob, cancelJob, addJob, getLogs, getMetrics, API } from './apiService'

function AddIcon ({ color = '#609' }) {
return (
Expand Down Expand Up @@ -189,7 +189,12 @@ function App() {
<h3>Job #{job.id}</h3>
{!job.finishedOn && (<Button background="silver" onClick={() => fetchJobs()}>Refresh</Button>)}
<div className="spacer"></div>
<Button background="limegreen" onClick={() => openLogs(job)}>Logs</Button>
<Button background="darkslateblue" onClick={() => openLogs(job)}>Logs</Button>
{job.returnvalue && (
<a href={`${API}/files/${job.returnvalue}`} target="_blank">
<Button background="limegreen">Download</Button>
</a>
)}
{job.finishedOn ? (
<Button background="indianred" onClick={() => onDelete(job)}>Remove</Button>
) : (
Expand Down

0 comments on commit 4c729fd

Please sign in to comment.