-
-
Notifications
You must be signed in to change notification settings - Fork 766
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Remove attachments from public access
Closes #219
- Loading branch information
Showing
10 changed files
with
156 additions
and
19 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
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
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
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,67 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const Errors = { | ||
ATTACHMENT_NOT_FOUND: { | ||
attachmentNotFound: 'Attachment not found', | ||
}, | ||
}; | ||
|
||
module.exports = { | ||
inputs: { | ||
id: { | ||
type: 'string', | ||
regex: /^[0-9]+$/, | ||
required: true, | ||
}, | ||
filename: { | ||
type: 'string', | ||
required: true, | ||
}, | ||
}, | ||
|
||
exits: { | ||
attachmentNotFound: { | ||
responseType: 'notFound', | ||
}, | ||
}, | ||
|
||
async fn(inputs, exits) { | ||
const { currentUser } = this.req; | ||
|
||
const { attachment, card, project } = await sails.helpers.attachments | ||
.getProjectPath(inputs.id) | ||
.intercept('pathNotFound', () => Errors.ATTACHMENT_NOT_FOUND); | ||
|
||
const isBoardMember = await sails.helpers.users.isBoardMember(currentUser.id, card.boardId); | ||
|
||
if (!isBoardMember) { | ||
const isProjectManager = await sails.helpers.users.isProjectManager( | ||
currentUser.id, | ||
project.id, | ||
); | ||
|
||
if (!isProjectManager) { | ||
throw Errors.ATTACHMENT_NOT_FOUND; // Forbidden | ||
} | ||
} | ||
|
||
if (!attachment.isImage) { | ||
throw Errors.ATTACHMENT_NOT_FOUND; | ||
} | ||
|
||
const filePath = path.join( | ||
sails.config.custom.attachmentsPath, | ||
attachment.dirname, | ||
'thumbnails', | ||
inputs.filename, | ||
); | ||
|
||
if (!fs.existsSync(filePath)) { | ||
throw Errors.ATTACHMENT_NOT_FOUND; | ||
} | ||
|
||
this.res.setHeader('Content-Disposition', `inline; ${inputs.filename}`); | ||
return exits.success(fs.createReadStream(filePath)); | ||
}, | ||
}; |
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,69 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const Errors = { | ||
ATTACHMENT_NOT_FOUND: { | ||
attachmentNotFound: 'Attachment not found', | ||
}, | ||
}; | ||
|
||
module.exports = { | ||
inputs: { | ||
id: { | ||
type: 'string', | ||
regex: /^[0-9]+$/, | ||
required: true, | ||
}, | ||
filename: { | ||
type: 'string', | ||
required: true, | ||
}, | ||
}, | ||
|
||
exits: { | ||
attachmentNotFound: { | ||
responseType: 'notFound', | ||
}, | ||
}, | ||
|
||
async fn(inputs, exits) { | ||
const { currentUser } = this.req; | ||
|
||
const { attachment, card, project } = await sails.helpers.attachments | ||
.getProjectPath(inputs.id) | ||
.intercept('pathNotFound', () => Errors.ATTACHMENT_NOT_FOUND); | ||
|
||
const isBoardMember = await sails.helpers.users.isBoardMember(currentUser.id, card.boardId); | ||
|
||
if (!isBoardMember) { | ||
const isProjectManager = await sails.helpers.users.isProjectManager( | ||
currentUser.id, | ||
project.id, | ||
); | ||
|
||
if (!isProjectManager) { | ||
throw Errors.ATTACHMENT_NOT_FOUND; // Forbidden | ||
} | ||
} | ||
|
||
const filePath = path.join( | ||
sails.config.custom.attachmentsPath, | ||
attachment.dirname, | ||
attachment.filename, | ||
); | ||
|
||
if (!fs.existsSync(filePath)) { | ||
throw Errors.ATTACHMENT_NOT_FOUND; | ||
} | ||
|
||
let contentDisposition; | ||
if (attachment.isImage || path.extname(attachment.filename) === '.pdf') { | ||
contentDisposition = 'inline'; | ||
} else { | ||
contentDisposition = `attachment; ${inputs.filename}`; | ||
} | ||
|
||
this.res.setHeader('Content-Disposition', contentDisposition); | ||
return exits.success(fs.createReadStream(filePath)); | ||
}, | ||
}; |
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
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
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
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
File renamed without changes.