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