index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. const express = require('express');
  2. const path = require('path');
  3. const Module = require('../../base/Module');
  4. const RepositorySystem = require('../../systems/RepositorySystem');
  5. const RoutingSystem = require('../../systems/RoutingSystem');
  6. const uuidv4 = require('uuid/v4');
  7. const formidable = require('formidable');
  8. const fs = require('fs');
  9. let FileScheme = {
  10. fields: [
  11. {
  12. key: "filename",
  13. type: "text"
  14. },
  15. {
  16. key: "userId",
  17. type: "integer"
  18. },
  19. {
  20. key: "filetype",
  21. type: "text"
  22. }
  23. ]
  24. };
  25. class FileModule extends Module {
  26. constructor(){
  27. super('/files', [2,0,3,4,0], 'Files');
  28. this.repository = RepositorySystem.create('Files', FileScheme);
  29. this.setRepo(this.repository);
  30. this.FileSystemPath = process.cwd();
  31. this.path = this.FileSystemPath + '/localfiles';
  32. this.router = express.Router();
  33. this.init();
  34. }
  35. init() {
  36. let routes = [
  37. {
  38. type: 'custom',
  39. method: 'post',
  40. endpoint: '/files',
  41. customFn: (req,res) => {
  42. this.acceptFile(req)
  43. .then((data) => {
  44. res.send(data);
  45. })
  46. .catch((e) => {
  47. res.send(e);
  48. });
  49. }
  50. },
  51. {
  52. type:'custom',
  53. method: 'get',
  54. endpoint:'/files/:id',
  55. customFn: (req,res) =>{
  56. this.getFile(req.params)
  57. .then((results)=>{
  58. res.download(results[0],results[1]);
  59. })
  60. .catch((e)=>{
  61. res.send(e)
  62. })
  63. }
  64. }
  65. ];
  66. RoutingSystem.loadRoutes(routes, this.router);
  67. }
  68. setRepo(repo) {
  69. this._repository = repo;
  70. }
  71. _getFolder(type){
  72. switch(type){
  73. case 'sql':
  74. return 'SQL';
  75. case 'doc' || 'docx':
  76. return 'Word';
  77. case 'xlsx' || 'xlsm' || 'xlsb':
  78. return 'Excel';
  79. case 'jpg' || 'png':
  80. return 'Images';
  81. case 'txt':
  82. return 'txt';
  83. case 'pdf':
  84. return 'PDF';
  85. }
  86. }
  87. acceptFile(req, path = this.path){
  88. return new Promise((resolve,reject)=>{
  89. try{
  90. var form = new formidable.IncomingForm({uploadDir:path});
  91. form.parse(req,(err,fields,files)=>{
  92. if(err){
  93. reject(err)
  94. }
  95. Object.keys(files).map((key,index) =>{
  96. let type = files[key]['name'].split(".")[1]
  97. let FileName = form.openedFiles[0].name
  98. let newPath = path +'/'+ this._getFolder(type) +'/'+ FileName;
  99. fs.rename(files[key]['path'],newPath, (err) => {
  100. if (err) reject(err);
  101. let data = {
  102. "filename":FileName,
  103. "userId":null,
  104. "filetype":type,
  105. }
  106. this.repository.insert(data)
  107. .then((data)=>{
  108. console.log('h?')
  109. resolve(FileName);
  110. })
  111. .catch((e)=>{
  112. reject(e)
  113. })
  114. });
  115. });
  116. });
  117. }
  118. catch(e){
  119. reject(e);
  120. }
  121. });
  122. }
  123. getFile(filename){
  124. return new Promise((resolve,reject)=>{
  125. this.repository.get('filename',filename)
  126. .then((data)=>{
  127. let path = this.path + '/' + data[0].filetype.toUpperCase() +'/'+data[0].filename;
  128. let results = [ path,data[0].filename];
  129. resolve(results);
  130. })
  131. })
  132. .catch((e)=> reject(e))
  133. }
  134. }
  135. module.exports = FileModule;