mirror of
https://github.com/DeviceFarmer/stf.git
synced 2026-04-19 02:53:27 +02:00
Implement APK uploads using the new storage system. Installation from URL still does not work, and dropping the file on the screen may not work either.
This commit is contained in:
@@ -3,11 +3,14 @@ var util = require('util')
|
||||
var path = require('path')
|
||||
|
||||
var express = require('express')
|
||||
var validator = require('express-validator')
|
||||
var formidable = require('formidable')
|
||||
var Promise = require('bluebird')
|
||||
|
||||
var logger = require('../../util/logger')
|
||||
var Storage = require('../../util/storage')
|
||||
var requtil = require('../../util/requtil')
|
||||
var download = require('../../util/download')
|
||||
|
||||
module.exports = function(options) {
|
||||
var log = logger.createLogger('storage:temp')
|
||||
@@ -19,19 +22,77 @@ module.exports = function(options) {
|
||||
app.set('case sensitive routing', true)
|
||||
app.set('trust proxy', true)
|
||||
|
||||
app.use(express.json())
|
||||
app.use(validator())
|
||||
|
||||
storage.on('timeout', function(id) {
|
||||
log.info('Cleaning up inactive resource "%s"', id)
|
||||
})
|
||||
|
||||
app.post('/api/v1/s/:type/download', function(req, res) {
|
||||
requtil.validate(req, function() {
|
||||
req.checkBody('url').notEmpty()
|
||||
})
|
||||
.then(function() {
|
||||
return download(req.body.url, {
|
||||
dir: options.cacheDir
|
||||
})
|
||||
})
|
||||
.then(function(file) {
|
||||
return {
|
||||
id: storage.store(file)
|
||||
, name: file.name
|
||||
}
|
||||
})
|
||||
.then(function(file) {
|
||||
res.status(201)
|
||||
.json({
|
||||
success: true
|
||||
, resource: {
|
||||
date: new Date()
|
||||
, type: req.params.type
|
||||
, id: file.id
|
||||
, name: file.name
|
||||
, href: util.format(
|
||||
'/api/v1/s/%s/%s%s'
|
||||
, req.params.type
|
||||
, file.id
|
||||
, file.name
|
||||
? util.format('/%s', path.basename(file.name))
|
||||
: ''
|
||||
)
|
||||
}
|
||||
})
|
||||
})
|
||||
.catch(requtil.ValidationError, function(err) {
|
||||
res.status(400)
|
||||
.json({
|
||||
success: false
|
||||
, error: 'ValidationError'
|
||||
, validationErrors: err.errors
|
||||
})
|
||||
})
|
||||
.catch(function(err) {
|
||||
log.error('Error storing resource', err.stack)
|
||||
res.status(500)
|
||||
.json({
|
||||
success: false
|
||||
, error: 'ServerError'
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
app.post('/api/v1/s/:type', function(req, res) {
|
||||
var form = new formidable.IncomingForm()
|
||||
Promise.promisify(form.parse, form)(req)
|
||||
.spread(function(fields, files) {
|
||||
return Object.keys(files).map(function(field) {
|
||||
var file = files[field]
|
||||
log.info('Uploaded "%s" to "%s"', file.name, file.path)
|
||||
return {
|
||||
field: field
|
||||
, id: storage.store(files[field])
|
||||
, name: files[field].name
|
||||
, id: storage.store(file)
|
||||
, name: file.name
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user