-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
1,851 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.