├── .env.defaults ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── index.js ├── package.json └── sdk ├── LocalStorage.js ├── Router.js ├── S3Storage.js └── index.js /.env.defaults: -------------------------------------------------------------------------------- 1 | PORT=8081 2 | 3 | # if empty then require('os').tmpdir() is used 4 | FILEBROWSER_UPLOAD_PATH=upload 5 | 6 | # if empty then require('os').homedir() is used 7 | FILEBROWSER_LOCAL_ROOT_PATH=files 8 | 9 | # AWS 10 | AWS_ACCESS_KEY_ID= 11 | AWS_SECRET_ACCESS_KEY= 12 | AWS_REGION= 13 | AWS_S3_BUCKET= 14 | 15 | # root AWS S3 prefix 16 | FILEBROWSER_AWS_ROOT_PATH= 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .env -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## **1.0.2** - *2019-09-26* 4 | * `FILEBROWSER_ROOT_PATH` environment variable renamed to `FILEBROWSER_LOCAL_ROOT_PATH`; 5 | * `FILEBROWSER_AWS_ROOT_PATH` environment variable added; 6 | 7 | ## **1.0.1** - *2019-09-22* 8 | * `README` updated; 9 | * some comments added; 10 | 11 | ## **1.0.0** - *2019-09-22* 12 | * initial release -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Yurii Semeniuk 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Vuetify File Browser Server & Backend SDK 2 | 3 | Backend for [Vuetify File Browser Component](https://www.npmjs.com/package/vuetify-file-browser) 4 | 5 | ## Usage 6 | 7 | ### As standalone server 8 | 9 | ```bash 10 | git clone https://github.com/semeniuk/vuetify-file-browser-server 11 | cd vuetify-file-browser-server 12 | cp .env.defaults .env 13 | ``` 14 | 15 | Then set configuration properties in `.env` file. After this run the server: 16 | 17 | ```bash 18 | npm start 19 | ``` 20 | 21 | ### As Express.js router 22 | 23 | ```bash 24 | npm i vuetify-file-browser-server 25 | ``` 26 | 27 | ```js 28 | const express = require("express"), 29 | app = express(), 30 | cors = require("cors"), 31 | bodyParser = require("body-parser"), 32 | path = require("path"), 33 | sdk = require("vuetify-file-browser-server/sdk"); 34 | 35 | // enable CORS 36 | app.use(cors()); 37 | 38 | // parse incoming request body 39 | app.use(bodyParser.urlencoded({ extended: true })); 40 | app.use(bodyParser.json()); 41 | 42 | // get AWS configuration from process.env 43 | const { AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION, AWS_S3_BUCKET, FILEBROWSER_AWS_ROOT_PATH } = process.env; 44 | 45 | // setup routes 46 | app.use("/storage", sdk.Router([ 47 | new sdk.LocalStorage(path.resolve(__dirname, "./files")), 48 | new sdk.S3Storage(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION, AWS_S3_BUCKET, FILEBROWSER_AWS_ROOT_PATH) 49 | ], { 50 | uploadPath: path.resolve(__dirname, "./upload") 51 | })); 52 | 53 | app.listen(process.env.PORT || 8081); 54 | ``` 55 | 56 | ### Custom implementation 57 | 58 | See [`vuetify-file-browser-server/sdk/Router`](https://github.com/semeniuk/vuetify-file-browser-server/blob/master/sdk/Router.js) for example how to use `LocalStorage` and `S3Storage` -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // change process' current dir 2 | process.chdir(__dirname); 3 | 4 | // load environment variables from `.env` 5 | require("dotenv-defaults").config(); 6 | 7 | const express = require('express'), 8 | bodyParser = require("body-parser"), 9 | cors = require("cors"), 10 | app = express(), 11 | port = process.env.PORT || 8081, 12 | os = require("os"), 13 | path = require("path"); 14 | 15 | // enable CORS 16 | app.use(cors()); 17 | 18 | // parse incoming request body 19 | app.use(bodyParser.urlencoded({ extended: true })); 20 | app.use(bodyParser.json()); 21 | 22 | // where temporary store files while uploading 23 | let uploadPath; 24 | if (process.env.FILEBROWSER_UPLOAD_PATH) { 25 | uploadPath = path.resolve(process.cwd(), process.env.FILEBROWSER_UPLOAD_PATH); 26 | } else { 27 | uploadPath = os.tmpdir(); 28 | } 29 | 30 | // get AWS configuration from process.env 31 | const { AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION, AWS_S3_BUCKET, FILEBROWSER_AWS_ROOT_PATH } = process.env; 32 | 33 | const LocalStorage = require("./sdk").LocalStorage; 34 | const S3Storage = require("./sdk").S3Storage; 35 | 36 | // setup routes 37 | app.use("/storage", require("./sdk").Router([ 38 | new LocalStorage(), 39 | new S3Storage(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION, AWS_S3_BUCKET, FILEBROWSER_AWS_ROOT_PATH) 40 | ], { 41 | uploadPath 42 | })); 43 | 44 | // home route 45 | app.get('/', (req, res) => res.send("Vuetify File Browser server")); 46 | 47 | app.listen(port, () => console.log(`Vuetify File Browser server listening on port ${port}!`)) -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vuetify-file-browser-server", 3 | "version": "1.0.2", 4 | "description": "Server for Vuetify File Browser", 5 | "main": "index.js", 6 | "scripts": { 7 | "mkdir": "mkdir upload && mkdir files && mkdir files/test", 8 | "start": "npm run mkdir && node index.js" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/semeniuk/vuetify-file-browser-server.git" 13 | }, 14 | "keywords": [ 15 | "File", 16 | "Browser", 17 | "Vue", 18 | "Vuetify", 19 | "Express" 20 | ], 21 | "author": "Yurii Semeniuk", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/semeniuk/vuetify-file-browser-server/issues" 25 | }, 26 | "homepage": "https://github.com/semeniuk/vuetify-file-browser-server#readme", 27 | "dependencies": { 28 | "aws-sdk": "^2.529.0", 29 | "body-parser": "^1.19.0", 30 | "cors": "^2.8.5", 31 | "dotenv-defaults": "^1.0.2", 32 | "express": "^4.17.1", 33 | "multer": "^1.4.2", 34 | "rimraf": "^3.0.0" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /sdk/LocalStorage.js: -------------------------------------------------------------------------------- 1 | const os = require("os"), 2 | nodePath = require("path"), 3 | fsPromises = require("fs").promises, 4 | readdir = fsPromises.readdir, 5 | stat = fsPromises.stat, 6 | rename = fsPromises.rename, 7 | unlink = fsPromises.unlink, 8 | lstat = fsPromises.lstat, 9 | util = require("util"), 10 | rimraf = util.promisify(require("rimraf")); 11 | 12 | 13 | class LocalStorage { 14 | constructor(root) { 15 | this.code = "local"; 16 | if (root) { 17 | this.root = root; 18 | } else if (process.env.FILEBROWSER_LOCAL_ROOT_PATH) { 19 | this.root = nodePath.resolve(process.cwd(), process.env.FILEBROWSER_LOCAL_ROOT_PATH); 20 | } else { 21 | this.root = os.homedir(); 22 | } 23 | } 24 | 25 | async list(path) { 26 | try { 27 | let dirs = [], 28 | files = []; 29 | 30 | if (path[path.length - 1] !== "/") { 31 | path += "/"; 32 | } 33 | let items = await readdir(this.root + path, { withFileTypes: true }); 34 | 35 | for (let item of items) { 36 | let isFile = item.isFile(), 37 | isDir = item.isDirectory(); 38 | 39 | if (!isFile && !isDir) { 40 | continue; 41 | } 42 | 43 | let result = { 44 | type: isFile ? "file" : "dir", 45 | path: path + item.name, 46 | }; 47 | 48 | result.basename = result.name = nodePath.basename(result.path); 49 | 50 | if (isFile) { 51 | let fileStat = await stat(this.root + result.path); 52 | result.size = fileStat.size; 53 | result.extension = nodePath.extname(result.path).slice(1); 54 | result.name = nodePath.basename(result.path, "." + result.extension); 55 | files.push(result); 56 | } else { 57 | result.path += "/"; 58 | dirs.push(result); 59 | } 60 | } 61 | 62 | return dirs.concat(files); 63 | } catch (err) { 64 | console.error(err); 65 | } 66 | } 67 | 68 | async upload(path, files) { 69 | try { 70 | for (let file of files) { 71 | await rename(file.path, this.root + path + file.originalname); 72 | } 73 | } catch (err) { 74 | console.error(err); 75 | } 76 | } 77 | 78 | async mkdir(path) { 79 | await fsPromises.mkdir(this.root + path, { recursive: true }); 80 | } 81 | 82 | async delete(path) { 83 | try { 84 | let stat = await lstat(this.root + path), 85 | isDir = stat.isDirectory(), 86 | isFile = stat.isFile(); 87 | 88 | if (isFile) { 89 | await unlink(this.root + path); 90 | } else if (isDir) { 91 | await rimraf(this.root + path); 92 | } 93 | } catch (err) { 94 | console.error(err); 95 | } 96 | } 97 | } 98 | 99 | module.exports = LocalStorage; -------------------------------------------------------------------------------- /sdk/Router.js: -------------------------------------------------------------------------------- 1 | const router = require("express").Router(), 2 | multer = require("multer"); 3 | 4 | module.exports = function (storages, options = {}) { 5 | let uploadPath = options.uploadPath || require("os").tmpdir(); 6 | for (let storage of storages) { 7 | 8 | // `list` endpoint 9 | router.get(`/${storage.code}/list`, async function (req, res) { 10 | let result = await storage.list(req.query.path); 11 | return res.json(result); 12 | }); 13 | 14 | // `upload` endpoint 15 | router.post(`/${storage.code}/upload`, multer({ dest: uploadPath }).array("files"), async function (req, res) { 16 | await storage.upload(req.query.path, req.files); 17 | return res.sendStatus(200); 18 | }); 19 | 20 | // `mkdir` endpoint 21 | router.post(`/${storage.code}/mkdir`, async function (req, res) { 22 | await storage.mkdir(req.query.path, req.query.name); 23 | return res.sendStatus(200); 24 | }); 25 | 26 | // `delete` endpoint 27 | router.post(`/${storage.code}/delete`, async function (req, res) { 28 | await storage.delete(req.query.path); 29 | return res.sendStatus(200); 30 | }); 31 | } 32 | return router; 33 | } 34 | -------------------------------------------------------------------------------- /sdk/S3Storage.js: -------------------------------------------------------------------------------- 1 | const nodePath = require("path"), 2 | AWS = require("aws-sdk"); 3 | 4 | class S3Storage { 5 | constructor(accessKeyId, secretKey, region, bucket, rootPath) { 6 | this.code = "s3"; 7 | AWS.config.update({ 8 | accessKeyId: accessKeyId, 9 | secretAccessKey: secretKey, 10 | region: region 11 | }); 12 | 13 | this.S3 = new AWS.S3({ 14 | apiVersion: "2006-03-01", 15 | params: { Bucket: bucket } 16 | }); 17 | 18 | if (rootPath && rootPath[0] === "/") { 19 | rootPath = rootPath.slice(1); 20 | } 21 | 22 | if (rootPath && rootPath[rootPath.length - 1] !== "/") { 23 | rootPath += "/"; 24 | } 25 | 26 | this.rootPath = rootPath; 27 | } 28 | 29 | async list(path) { 30 | try { 31 | let dirs = [], 32 | files = []; 33 | 34 | let data = await this.S3.listObjectsV2({ 35 | Delimiter: "/", 36 | Prefix: this.rootPath + path.slice(1) 37 | }).promise(); 38 | 39 | for (let prefix of data.CommonPrefixes) { 40 | let dir = { 41 | type: "dir", 42 | path: "/" + prefix.Prefix.slice(this.rootPath.length) 43 | }; 44 | dir.basename = dir.name = nodePath.basename(dir.path); 45 | dirs.push(dir); 46 | } 47 | 48 | for (let item of data.Contents.filter(item => item.Key != data.Prefix)) { 49 | let file = { 50 | type: "file", 51 | path: "/" + item.Key.slice(this.rootPath.length), 52 | size: item.Size, 53 | lastModified: item.LastModified, 54 | eTag: item.ETag 55 | }; 56 | file.basename = nodePath.basename(file.path); 57 | file.extension = nodePath.extname(file.path).slice(1); 58 | file.name = nodePath.basename(file.path, "." + file.extension); 59 | files.push(file); 60 | } 61 | return dirs.concat(files); 62 | 63 | } catch (err) { 64 | console.error(err); 65 | } 66 | } 67 | 68 | async upload(path, files) { 69 | try { 70 | const fs = require("fs"); 71 | path = this.rootPath + path.slice(1); 72 | 73 | for (let file of files) { 74 | var fileStream = fs.createReadStream(file.path); 75 | await this.S3.upload({ 76 | Key: path + file.originalname, 77 | Body: fileStream 78 | }).promise(); 79 | } 80 | } catch (err) { 81 | console.error(err); 82 | } 83 | } 84 | 85 | async mkdir(path) { 86 | path = this.rootPath + path.slice(1) + "/"; 87 | await this.S3.upload({ 88 | Key: path, 89 | Body: "" 90 | }).promise(); 91 | } 92 | 93 | async deleteFile(key) { 94 | await this.S3.deleteObject({ Key: this.rootPath + key }).promise(); 95 | } 96 | 97 | async deleteDir(prefix) { 98 | const listedObjects = await this.S3.listObjectsV2({ 99 | Prefix: this.rootPath + prefix 100 | }).promise(); 101 | 102 | if (listedObjects.Contents.length === 0) { 103 | return; 104 | } 105 | 106 | const deleteParams = { 107 | Delete: { Objects: [] } 108 | }; 109 | 110 | listedObjects.Contents.forEach(({ Key }) => { 111 | deleteParams.Delete.Objects.push({ Key }); 112 | }); 113 | 114 | await this.S3.deleteObjects(deleteParams).promise(); 115 | 116 | if (listedObjects.IsTruncated) { 117 | await this.deleteDir(prefix); 118 | } 119 | } 120 | 121 | async delete(path) { 122 | try { 123 | path = path.slice(1); 124 | if (path[path.length - 1] == "/") { 125 | await this.deleteDir(path); 126 | } else { 127 | await this.deleteFile(path); 128 | } 129 | } catch (err) { 130 | console.error(err); 131 | } 132 | } 133 | } 134 | 135 | module.exports = S3Storage; -------------------------------------------------------------------------------- /sdk/index.js: -------------------------------------------------------------------------------- 1 | const Router = require("./Router"), 2 | LocalStorage = require("./LocalStorage"), 3 | S3Storage = require("./S3Storage"); 4 | 5 | module.exports = { 6 | LocalStorage, 7 | S3Storage, 8 | Router 9 | } --------------------------------------------------------------------------------