upload_caches_controller.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class UploadCachesController < ApplicationController
  3. prepend_before_action :authenticate_and_authorize!
  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. created_by_id: current_user.id
  20. )
  21. # return result
  22. render json: {
  23. success: true,
  24. data: {
  25. id: store.id, # TODO: rename?
  26. filename: file.original_filename,
  27. size: store.size,
  28. contentType: store.preferences['Content-Type']
  29. }
  30. }
  31. end
  32. # DELETE /upload_caches/1
  33. def destroy
  34. cache.destroy
  35. render json: {
  36. success: true,
  37. }
  38. end
  39. # DELETE /upload_caches/1/items/1
  40. def remove_item
  41. cache.remove_item(params[:store_id])
  42. render json: {
  43. success: true,
  44. }
  45. end
  46. private
  47. def cache
  48. UploadCache.new(params[:id])
  49. end
  50. end