-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate attachment-from-file function
- Loading branch information
Showing
2 changed files
with
26 additions
and
20 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,24 @@ | ||
const fs = require('./sync-fs'); | ||
|
||
module.exports = path => { | ||
const data = fs.readBinary(path); | ||
const mime = mimeTypeFor(path); | ||
return { | ||
content_type: mime, | ||
data: new Buffer(data), | ||
}; | ||
}; | ||
|
||
function mimeTypeFor(fileName) { | ||
const extensionStart = fileName.indexOf('.'); | ||
const extension = extensionStart === -1 ? | ||
fileName : | ||
fileName.substring(extensionStart+1); | ||
|
||
switch(extension) { | ||
case 'json': return 'application/json'; | ||
case 'png' : return 'image/png'; | ||
case 'xml' : return 'application/xml'; | ||
default: throw new Error(`Unrecongised file extension: ${extension}`); | ||
} | ||
} |
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