├── .gitignore
├── Documentation.docx
├── LICENSE
├── README.md
├── app.js
├── package.json
├── public
├── css
│ └── style.css
├── img
│ └── 200.gif
└── js
│ ├── getfilelist.js
│ └── uploader.js
├── setup project (Linux).sh
├── setup project (windows).bat
└── view
└── index.html
/.gitignore:
--------------------------------------------------------------------------------
1 | # node
2 | node_modules
3 | package-lock.json
4 |
5 | # user files
6 | userfiles
7 |
8 | # documentation files
9 | ~$cumentation.docx
10 |
11 | # temps
12 | *.tmp
--------------------------------------------------------------------------------
/Documentation.docx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/GamEditor/Node.js-FileManager/3d01c74969a46c407163902dabe175cb56c15b1d/Documentation.docx
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 GamEditors
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 | # Node.js-FileManager
2 | This is a simple file manager for download and upload files on the server by categories. the file manager is made up with Node.js and Express. you can change it, improve it and publish it for yourself.
3 |
4 | also you can use this simple file server for sharing files on a local network with every devices (such as desktop and mobiles) too!
5 |
6 | Please follow me on instagram
7 |
--------------------------------------------------------------------------------
/app.js:
--------------------------------------------------------------------------------
1 | const express = require('express');
2 | const app = express();
3 | const fs = require('fs');
4 | const multer = require('multer');
5 |
6 | const port = 8000;
7 | const responsedelay = 50; // miliseconds
8 |
9 | // static folders
10 | app.use(express.static('public'));
11 | app.use(express.static('userfiles'));
12 | app.use(express.static('view'));
13 |
14 | // home page
15 | app.get('/', function(req, res)
16 | {
17 | res.sendFile('index.html');
18 | });
19 |
20 | // upload handler
21 | var uploadStorage = multer.diskStorage(
22 | {
23 | destination: function (req, file, cb)
24 | {
25 | cb(null, `./userfiles/${req.query.type}`); ///${req.folder}`);
26 | },
27 | filename: function (req, file, cb)
28 | {
29 | //let fileName = checkFileExistense(req.query.folder ,file.originalname);
30 | cb(null, file.originalname);
31 | }
32 | });
33 |
34 | var upload = multer({ storage: uploadStorage });
35 |
36 | app.post('/', upload.single('file'), function(req, res)
37 | {
38 | console.log(req.file);
39 | console.log('file upload...');
40 | });
41 |
42 | // all type of files except images will explored here
43 | app.post('/files-list', function(req, res)
44 | {
45 | let folder = req.query.folder;
46 | let contents = '';
47 |
48 | let readingdirectory = `./userfiles/${folder}`;
49 |
50 | fs.readdir(readingdirectory, function(err, files)
51 | {
52 | if(err) { console.log(err); }
53 | else if(files.length > 0)
54 | {
55 | files.forEach(function(value, index, array)
56 | {
57 | fs.stat(`${readingdirectory}/${value}`, function(err, stats)
58 | {
59 | let filesize = ConvertSize(stats.size);
60 | contents += '
' + '\n';
61 |
62 | if(index == (array.length - 1)) { setTimeout(function() {res.send(contents);}, responsedelay); }
63 | });
64 | });
65 | }
66 | else
67 | {
68 | setTimeout(function() {res.send(contents);}, responsedelay);
69 | }
70 | });
71 | });
72 |
73 | /*
74 | this part is written because i wanted to show you how you can explore in differnt directories
75 | for search a specific type of files (in this case images)
76 | */
77 | app.post('/image-list', function(req, res)
78 | {
79 | // this part is a little different because i wanted to find all images without looking at file extentions
80 | // and instead i will looking for image files due to the binary information of files and check several first bytes
81 | // those bytes are unique for each file type (or mime type)
82 |
83 | var contents = '';
84 |
85 | var directories = ['/font', '/html', '/image', '/pdf', '/video'];
86 | var dirindex = 0;
87 |
88 | /**
89 | * this is an inline function for iterating the directories array instead of loops. it cause avoiding conflitcness of loops and async jobs.
90 | * @param dindex index of directories
91 | */
92 | var readNextDirectory = function(dindex)
93 | {
94 | let directory = directories[dindex];
95 |
96 | fs.readdir(`./userfiles${directory}`, function(err, files)
97 | {
98 | if(err)
99 | {
100 | console.log(`Error: ${err}`);
101 | }
102 | else if(files.length > 0)
103 | {
104 | files.forEach(function(value, index, array)
105 | {
106 | fs.readFile(`./userfiles${directory}/${value}`, function(err, data)
107 | {
108 | if(err)
109 | {
110 | console.log(err);
111 | }
112 | // ffd8 and 89504e47 are magic number of image files (jpeg and png)
113 | // this condition searches for "jpeg" and "png" images (you must add other magic numbers yourself)
114 | else if(data.toString('hex').search('ffd8') == 0 || data.toString('hex').search('89504e47') == 0) // findig jpeg and png images by watching files as binary data and checking the 2 or 4 magic bytes/numbers.
115 | {
116 | fs.stat(`./userfiles${directory}/${value}`, function(err, stats)
117 | {
118 | let filesize = ConvertSize(stats.size);
119 | contents += '