Skip to content

Commit

Permalink
Merge b419520 into 7a7f548
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro-Gonzalez committed Dec 19, 2019
2 parents 7a7f548 + b419520 commit bd16274
Show file tree
Hide file tree
Showing 23 changed files with 1,851 additions and 209 deletions.
348 changes: 306 additions & 42 deletions README.md

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions lib/base-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const Model = require('@janiscommerce/model');

class BaseFileModel extends Model {
static get fields() {
return {
id: true,
path: true,
size: true,
name: true,
type: true,
dateCreated: true
};
}

static get table() {
return 'files';
}
}

module.exports = BaseFileModel;
16 changes: 15 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
'use strict';

const BaseFileModel = require('./base-model');
const SlsApiUpload = require('./sls-api-upload');
const SlsApiFileRelation = require('./sls-api-file-relation');
const SlsApiFileDelete = require('./sls-api-file-delete');
const SlsApiFileGet = require('./sls-api-file-get');
const SlsApiFileList = require('./sls-api-file-list');
const SlsApiFileRelationError = require('./sls-api-file-relation-error');
const SlsApiUploadError = require('./sls-api-upload-error');
const SlsApiFileDeleteError = require('./sls-api-file-delete-error');
const SlsApiFileGetError = require('./sls-api-file-get-error');

module.exports = {
BaseFileModel,
SlsApiUpload,
SlsApiFileRelation,
SlsApiUploadError
SlsApiFileDelete,
SlsApiFileGet,
SlsApiFileList,
SlsApiFileRelationError,
SlsApiUploadError,
SlsApiFileDeleteError,
SlsApiFileGetError
};
14 changes: 0 additions & 14 deletions lib/s3/index.js

This file was deleted.

15 changes: 0 additions & 15 deletions lib/s3/s3Wrapper.js

This file was deleted.

29 changes: 29 additions & 0 deletions lib/sls-api-file-delete-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

class SlsApiFileDeleteError extends Error {

static get messages() {

return {
BUCKET_NOT_DEFINED: 'Bucket not defined',
BUCKET_NOT_STRING: 'Bucket should be return a string',
ENTITY_ID_FIELD_NOT_DEFINED: 'EntityIdField not defined',
ENTITY_ID_FIELD_NOT_STRING: 'EntityIdField should be return a string',
MODEL_NOT_DEFINED: 'Model not defined',
MODEL_IS_NOT_MODEL_CLASS: 'Model is not a Model Class',
FILE_RECORD_NOT_FOUND: 'file record not found'
};

}

constructor(err) {
super(err.message || err);

this.name = 'SlsApiFileDeleteError';

if(err instanceof Error)
this.previousError = err;
}
}

module.exports = SlsApiFileDeleteError;
57 changes: 57 additions & 0 deletions lib/sls-api-file-delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

const S3 = require('@janiscommerce/s3');
const { API } = require('@janiscommerce/api');
const SlsApiFileDeleteError = require('./sls-api-file-delete-error');

class SlsApiFileDelete extends API {

async validate() {
if(!this.model)
throw new SlsApiFileDeleteError(SlsApiFileDeleteError.messages.MODEL_NOT_DEFINED);

if(typeof this.model !== 'function')
throw new SlsApiFileDeleteError(SlsApiFileDeleteError.messages.MODEL_IS_NOT_MODEL_CLASS);

if(!this.entityIdField)
throw new SlsApiFileDeleteError(SlsApiFileDeleteError.messages.ENTITY_ID_FIELD_NOT_DEFINED);

if(typeof this.entityIdField !== 'string')
throw new SlsApiFileDeleteError(SlsApiFileDeleteError.messages.ENTITY_ID_FIELD_NOT_STRING);

if(!this.bucket)
throw new SlsApiFileDeleteError(SlsApiFileDeleteError.messages.BUCKET_NOT_DEFINED);

if(typeof this.bucket !== 'string')
throw new SlsApiFileDeleteError(SlsApiFileDeleteError.messages.BUCKET_NOT_STRING);
}


async process() {
const [entityId, fileId] = this.pathParameters;

this.modelInstance = this.session.getSessionInstance(this.model);

const filtersParams = { filters: { [this.entityIdField]: entityId, id: fileId } };

const [record] = await this.modelInstance.get(filtersParams);

if(!record) {
this.setCode(404);
throw new SlsApiFileDeleteError(SlsApiFileDeleteError.messages.FILE_RECORD_NOT_FOUND);
}

await this.modelInstance.remove(filtersParams);

try {
await S3.deleteObject({ Bucket: this.bucket, Key: record.path });
} catch(error) {
if(error.statusCode !== 404)
throw new SlsApiFileDeleteError(error);
}
}

}


module.exports = SlsApiFileDelete;
29 changes: 29 additions & 0 deletions lib/sls-api-file-get-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

class SlsApiFileGetError extends Error {

static get messages() {

return {
BUCKET_NOT_DEFINED: 'Bucket not defined',
BUCKET_NOT_STRING: 'Bucket should be return a string',
ENTITY_ID_FIELD_NOT_DEFINED: 'EntityIdField not defined',
ENTITY_ID_FIELD_NOT_STRING: 'EntityIdField should be return a string',
MODEL_NOT_DEFINED: 'Model not defined',
MODEL_IS_NOT_MODEL_CLASS: 'Model is not a Model Class',
FILE_RECORD_NOT_FOUND: 'file record not found'
};

}

constructor(err) {
super(err.message || err);

this.name = 'SlsApiFileGetError';

if(err instanceof Error)
this.previousError = err;
}
}

module.exports = SlsApiFileGetError;
41 changes: 41 additions & 0 deletions lib/sls-api-file-get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

const S3 = require('@janiscommerce/s3');
const { ApiGet } = require('@janiscommerce/api-get');
const SlsApiFileGetError = require('./sls-api-file-get-error');

class SlsApiFileGet extends ApiGet {

validateBucket() {
if(!this.bucket)
throw new SlsApiFileGetError(SlsApiFileGetError.messages.BUCKET_NOT_DEFINED);

if(typeof this.bucket !== 'string')
throw new SlsApiFileGetError(SlsApiFileGetError.messages.BUCKET_NOT_STRING);
}

async format(record) {
this.validateBucket();

const { path, ...currentRecord } = record;

let url = null;

try {
url = await S3.getSignedUrl('getObject', {
Bucket: this.bucket,
Key: record.path,
ResponseContentDisposition: `attachment; filename="${currentRecord.name}"`
});
} catch(error) {
if(error.statusCode !== 404)
throw new SlsApiFileGetError(error);
}

return { ...currentRecord, url };
}

}


module.exports = SlsApiFileGet;
32 changes: 32 additions & 0 deletions lib/sls-api-file-list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

const { ApiListData } = require('@janiscommerce/api-list');

class FileListApi extends ApiListData {

get sortableFields() {
return [
'id',
'name',
'dateCreated'
];
}

get availableFilters() {
return [
'id',
'name',
{
name: 'dateCreated',
valueMapper: date => new Date(date)
}
];
}

formatRows(files) {
return files.map(({ path, ...file }) => file);
}

}

module.exports = FileListApi;
28 changes: 28 additions & 0 deletions lib/sls-api-file-relation-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

class SlsApiFileRelationError extends Error {

static get messages() {

return {
BUCKET_NOT_DEFINED: 'Bucket not defined',
BUCKET_NOT_STRING: 'Bucket should be return a string',
ENTITY_ID_FIELD_NOT_DEFINED: 'EntityIdField not defined',
ENTITY_ID_FIELD_NOT_STRING: 'EntityIdField should be return a string',
MODEL_NOT_DEFINED: 'Model not defined',
MODEL_IS_NOT_MODEL_CLASS: 'Model is not a Model Class'
};

}

constructor(err) {
super(err.message || err);

this.name = 'SlsApiFileRelationError';

if(err instanceof Error)
this.previousError = err;
}
}

module.exports = SlsApiFileRelationError;

0 comments on commit bd16274

Please sign in to comment.