file.controller.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * Created by zhengguorong on 16/11/4.
  3. */
  4. var jsonpatch = require('fast-json-patch')
  5. var File = require('./file.model')
  6. var tools = require('../../util/tools')
  7. var uuid = require('node-uuid')
  8. var fs = require('fs')
  9. var path = require('path')
  10. const respondWithResult = (res, statusCode) => {
  11. statusCode = statusCode || 200
  12. return function (entity) {
  13. if (entity) {
  14. return res.status(statusCode).json(entity)
  15. }
  16. return null
  17. }
  18. }
  19. const patchUpdates = (patches) => {
  20. return function (entity) {
  21. try {
  22. jsonpatch.apply(entity, patches, /*validate*/ true)
  23. } catch (err) {
  24. return Promise.reject(err)
  25. }
  26. return entity.save()
  27. }
  28. }
  29. const removeEntity = (res) => {
  30. return function (entity) {
  31. if (entity) {
  32. return entity.remove()
  33. .then(() => {
  34. res.status(204).end()
  35. })
  36. }
  37. }
  38. }
  39. const handleEntityNotFound = (res) => {
  40. return function (entity) {
  41. if (!entity) {
  42. res.status(404).end()
  43. return null
  44. }
  45. return entity
  46. }
  47. }
  48. const handleError = (res, statusCode) => {
  49. statusCode = statusCode || 500
  50. return function (err) {
  51. res.status(statusCode).send(err)
  52. }
  53. }
  54. module.exports.index = (req, res) => {
  55. return File.find().exec()
  56. .then(respondWithResult(res))
  57. .catch(handleError(res))
  58. }
  59. // Gets a single File from the DB
  60. module.exports.show = (req, res) => {
  61. return File.findById(req.params.id).exec()
  62. .then(handleEntityNotFound(res))
  63. .then(respondWithResult(res))
  64. .catch(handleError(res))
  65. }
  66. module.exports.getByThemeId = (req, res) => {
  67. return File.find({ themeId: req.params.id }).exec()
  68. .then(handleEntityNotFound(res))
  69. .then(respondWithResult(res))
  70. .catch(handleError(res))
  71. }
  72. // Creates a new File in the DB
  73. module.exports.create = (req, res) => {
  74. if (!req.files) {
  75. return res.status(400).send('No files were uploaded.');
  76. }
  77. let image = req.files.image;
  78. var pathInfo = buildImgPath(req.body.themeId || 'all')
  79. if (image) {
  80. var ext = '.' + image.mimetype.split('/')[1]
  81. image.mv(pathInfo.imagePath + ext, err => {
  82. if (err) {
  83. return res.status(500).send(err);
  84. }
  85. console.log(req.body.themeId, 'themeId')
  86. return File.create({filePath: pathInfo.accessPath + ext, themeId: req.body.themeId || ''})
  87. .then(respondWithResult(res, 201))
  88. .catch(handleError(res))
  89. })
  90. }
  91. }
  92. const buildImgPath = (themeId) => {
  93. // 文件使用uuid生成别名
  94. var fileName = uuid.v1().replace(/-/g, '')
  95. // 文件目录
  96. var dirPath = 'public/upload/' + themeId
  97. // 创建路径
  98. fs.existsSync(path.resolve(dirPath)) == false &&tools.mkdirs(dirPath)
  99. // 图片保存路径
  100. var imagePath = dirPath + '/' + fileName
  101. // 图片访问路径
  102. var accessPath = '/upload/' + themeId + '/' + fileName
  103. return { accessPath: accessPath, imagePath: imagePath, dirPath: dirPath }
  104. }
  105. // Deletes a File from the DB
  106. module.exports.destroy = (req, res) => {
  107. return File.findById(req.params.id).exec()
  108. .then(handleEntityNotFound(res))
  109. .then(removeEntity(res))
  110. .catch(handleError(res))
  111. }