c_file.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const fs = require('fs');
  2. const path = require('path');
  3. const handle = require('../until/handle')
  4. const field = require('../maps/field')
  5. const codeMap = require('../maps/rcodeMap')
  6. const enumMap = require('../maps/enum')
  7. const configPath = require('../configs/path.json')
  8. /**
  9. * 加载图片列表
  10. * @returns {Promise<*>}
  11. */
  12. async function loadImgs(){
  13. let [err,files] = await handle(fs.promises.readdir(configPath.upload))
  14. if(err){throw err}
  15. // 过滤非img内容
  16. return files.reduce(((a,c)=>{
  17. let ext = path.extname(c);
  18. if(enumMap.imageExt.includes(ext)){
  19. a.push({
  20. fileName:c,
  21. path: path.join(configPath.upload,c),
  22. })
  23. }else{
  24. }
  25. return a
  26. }),[])
  27. }
  28. async function toUpload (files){
  29. console.log(files)
  30. let file = files.file
  31. console.log(file)
  32. console.log(file.filepath)
  33. console.log(file.originalFilename)
  34. let targetPath = path.join(configPath.upload,file.originalFilename);
  35. let [err,res] = await handle(fs.promises.rename(file.filepath,targetPath))
  36. if(err){throw err}
  37. return true;
  38. }
  39. module.exports = {
  40. loadImgs,
  41. toUpload
  42. }