upload_caches_controller.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class UploadCachesController < ApplicationController
  3. prepend_before_action :authentication_check
  4. # POST /upload_caches/1
  5. def update
  6. file = params[:File]
  7. content_type = file.content_type
  8. if !content_type || content_type == 'application/octet-stream'
  9. mime_type = MIME::Types.type_for(file.original_filename).first
  10. content_type = mime_type&.content_type || 'application/octet-stream'
  11. end
  12. headers_store = {
  13. 'Content-Type' => content_type
  14. }
  15. store = cache.add(
  16. filename: file.original_filename,
  17. data: file.read,
  18. preferences: headers_store
  19. )
  20. # return result
  21. render json: {
  22. success: true,
  23. data: {
  24. id: store.id, # TODO: rename?
  25. filename: file.original_filename,
  26. size: store.size,
  27. }
  28. }
  29. end
  30. # DELETE /upload_caches/1
  31. def destroy
  32. cache.destroy
  33. render json: {
  34. success: true,
  35. }
  36. end
  37. # DELETE /upload_caches/1/items/1
  38. def remove_item
  39. cache.remove_item(params[:store_id])
  40. render json: {
  41. success: true,
  42. }
  43. end
  44. private
  45. def cache
  46. UploadCache.new(params[:id])
  47. end
  48. end