Adds content type support in S3
This commit is contained in:
@@ -10,7 +10,7 @@ var FCTestFactory = require("./FilesControllerTestFactory");
|
||||
describe("FilesController",()=>{
|
||||
|
||||
// Test the grid store adapter
|
||||
var gridStoreAdapter = new GridStoreAdapter();
|
||||
var gridStoreAdapter = new GridStoreAdapter('mongodb://localhost:27017/parse');
|
||||
FCTestFactory.testAdapter("GridStoreAdapter", gridStoreAdapter);
|
||||
|
||||
if (process.env.S3_ACCESS_KEY && process.env.S3_SECRET_KEY) {
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
// database adapter.
|
||||
|
||||
export class FilesAdapter {
|
||||
createFile(config, filename, data) { }
|
||||
createFile(config, filename: string, data, contentType: string) { }
|
||||
|
||||
deleteFile(config, filename) { }
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ export class GridStoreAdapter extends FilesAdapter {
|
||||
|
||||
// For a given config object, filename, and data, store a file
|
||||
// Returns a promise
|
||||
createFile(config, filename: string, data) {
|
||||
createFile(config, filename: string, data, contentType: string) {
|
||||
return this._connect().then(database => {
|
||||
let gridStore = new GridStore(database, filename, 'w');
|
||||
return gridStore.open();
|
||||
|
||||
@@ -68,7 +68,7 @@ export class S3Adapter extends FilesAdapter {
|
||||
|
||||
// For a given config object, filename, and data, store a file in S3
|
||||
// Returns a promise containing the S3 object creation response
|
||||
createFile(config, filename, data) {
|
||||
createFile(config, filename, data, contentType) {
|
||||
let params = {
|
||||
Key: this._bucketPrefix + filename,
|
||||
Body: data
|
||||
@@ -76,6 +76,9 @@ export class S3Adapter extends FilesAdapter {
|
||||
if (this._directAccess) {
|
||||
params.ACL = "public-read"
|
||||
}
|
||||
if (contentType) {
|
||||
params.ContentType = contentType;
|
||||
}
|
||||
return this.createBucket().then(() => {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._s3Client.upload(params, (err, data) => {
|
||||
|
||||
@@ -10,10 +10,10 @@ export class FilesController extends AdaptableController {
|
||||
return this.adapter.getFileData(config, filename);
|
||||
}
|
||||
|
||||
createFile(config, filename, data) {
|
||||
createFile(config, filename, data, contentType) {
|
||||
filename = randomHexString(32) + '_' + filename;
|
||||
var location = this.adapter.getFileLocation(config, filename);
|
||||
return this.adapter.createFile(config, filename, data).then(() => {
|
||||
return this.adapter.createFile(config, filename, data, contentType).then(() => {
|
||||
return Promise.resolve({
|
||||
url: location,
|
||||
name: filename
|
||||
|
||||
@@ -4,6 +4,7 @@ import * as Middlewares from '../middlewares';
|
||||
import { randomHexString } from '../cryptoUtils';
|
||||
import mime from 'mime';
|
||||
import Config from '../Config';
|
||||
import path from 'path';
|
||||
|
||||
export class FilesRouter {
|
||||
|
||||
@@ -66,20 +67,25 @@ export class FilesRouter {
|
||||
'Filename contains invalid characters.'));
|
||||
return;
|
||||
}
|
||||
let extension = '';
|
||||
|
||||
// Not very safe there.
|
||||
const hasExtension = req.params.filename.indexOf('.') > 0;
|
||||
const contentType = req.get('Content-type');
|
||||
let filename = req.params.filename;
|
||||
|
||||
// safe way to get the extension
|
||||
let extname = path.extname(filename);
|
||||
let contentType = req.get('Content-type');
|
||||
|
||||
const hasExtension = extname.length > 0;
|
||||
|
||||
if (!hasExtension && contentType && mime.extension(contentType)) {
|
||||
extension = '.' + mime.extension(contentType);
|
||||
filename = filename + '.' + mime.extension(contentType);
|
||||
} else if (hasExtension && !contentType) {
|
||||
contentType = mime.lookup(req.params.filename);
|
||||
}
|
||||
|
||||
const filename = req.params.filename + extension;
|
||||
const config = req.config;
|
||||
const filesController = config.filesController;
|
||||
|
||||
filesController.createFile(config, filename, req.body).then((result) => {
|
||||
filesController.createFile(config, filename, req.body, contentType).then((result) => {
|
||||
res.status(201);
|
||||
res.set('Location', result.url);
|
||||
res.json(result);
|
||||
|
||||
Reference in New Issue
Block a user