file.controller.js 2.8 KB

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