summaryrefslogtreecommitdiff
path: root/app/node_modules/okservices/oks3/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'app/node_modules/okservices/oks3/index.js')
-rw-r--r--app/node_modules/okservices/oks3/index.js145
1 files changed, 145 insertions, 0 deletions
diff --git a/app/node_modules/okservices/oks3/index.js b/app/node_modules/okservices/oks3/index.js
new file mode 100644
index 0000000..cc40b71
--- /dev/null
+++ b/app/node_modules/okservices/oks3/index.js
@@ -0,0 +1,145 @@
+var upload = require("./upload")
+var multer = require('multer')
+
+// Hack to prevent this god-forsaken module from crashing our shit
+var d = require('domain').create()
+d.on('error', function (err) {
+ console.log(err)
+ console.error('Stupid error in S3 upload. Upload probably prematurely canceled')
+})
+
+function OKS3(options) {
+ if (!(this instanceof OKS3)) return new OKS3(options);
+ options = options || {};
+ if (!options.express)
+ throw new Error('Express not provided to OKS3');
+ if (!options.s3)
+ throw new Error('S3 configuration not provided to OKS3');
+
+ if (!options.s3.image) options.s3.image = {}
+ if (!options.s3.audio) options.s3.audio = {}
+ if (!options.s3.video) options.s3.video = {}
+
+ // Make sure maxbytes property is there - it can be a number,
+ // or zero/undefined (for no maximum upload size)
+ if (options.s3.maxbytes) {
+ if (! ('maxbytes' in options.s3.image))
+ options.s3.image.maxbytes = options.s3.maxbytes
+ if (! ('maxbytes' in options.s3.video))
+ options.s3.video.maxbytes = options.s3.maxbytes
+ if (! ('maxbytes' in options.s3.audio))
+ options.s3.audio.maxbytes = options.s3.maxbytes
+ }
+ if (typeof options.s3.image.allowed !== "boolean")
+ options.s3.image.allowed = true
+ if (typeof options.s3.video.allowed !== "boolean")
+ options.s3.video.allowed = false
+ if (typeof options.s3.audio.allowed !== "boolean")
+ options.s3.audio.allowed = false
+
+ upload.init({
+ key: options.s3.key,
+ secret: options.s3.secret,
+ bucket: options.s3.bucket,
+ })
+
+ var express = options.express;
+
+ var router = express.Router();
+
+ var mult = multer()
+
+ router.post('/image', mult.single('image'), function(req, res) {
+ d.run(function () {
+
+ if (! options.s3.image.allowed) {
+ return res.status(500).json({ error: "Image uploading not permitted" })
+ }
+
+ upload.put({
+ file: req.file,
+ preserveFilename: options.s3.image.preserveFilename,
+ dirname: options.s3.dirname,
+ types: {
+ 'image/gif': 'gif',
+ 'image/jpeg': 'jpg',
+ 'image/jpg': 'jpg',
+ 'image/png': 'png',
+ },
+ unacceptable: function(err){
+ res.json({ error: err })
+ },
+ success: function(url){
+ res.json({ url: url })
+ }
+ })
+
+ });
+ });
+
+ router.post('/audio', mult.single('audio'), function(req, res) {
+ d.run(function () {
+
+ if (! options.s3.image.allowed) {
+ return res.status(500).json({ error: "Audio uploading not permitted" })
+ }
+
+ upload.put({
+ file: req.file,
+ preserveFilename: options.s3.audio.preserveFilename,
+ dirname: options.s3.dirname,
+ types: {
+ 'audio/mp3': 'mp3',
+ 'audio/mpeg': 'mp3',
+ 'audio/wav': 'wav',
+ 'audio/flac': 'flac',
+ },
+ unacceptable: function(err){
+ res.json({ error: err })
+ },
+ success: function(url){
+ res.json({ url: url })
+ }
+ })
+
+ });
+ });
+
+ router.post('/video', mult.single('video'), function(req, res) {
+ d.run(function () {
+
+ if (! options.s3.image.allowed) {
+ return res.status(500).json({ error: "Video uploading not permitted" })
+ }
+
+ upload.put({
+ file: req.file,
+ preserveFilename: options.s3.video.preserveFilename,
+ dirname: options.s3.dirname,
+ types: {
+ 'video/mp4': 'mp4',
+ 'video/webm': 'webm',
+ },
+ unacceptable: function(err){
+ res.json({ error: err })
+ },
+ success: function(url){
+ res.json({ url: url })
+ }
+ })
+
+ });
+ });
+
+ function preserveFilename (stream, cb){
+ cb(null, stream.filename)
+ }
+
+ this._middleware = router;
+}
+
+OKS3.prototype.middleware = function() {
+ return this._middleware;
+};
+
+module.exports = OKS3