upload_caches_controller.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # Copyright (C) 2012-2024 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. contentType: store.preferences['Content-Type']
  28. }
  29. }
  30. end
  31. # DELETE /upload_caches/1
  32. def destroy
  33. cache.destroy
  34. render json: {
  35. success: true,
  36. }
  37. end
  38. # DELETE /upload_caches/1/items/1
  39. def remove_item
  40. cache.remove_item(params[:store_id])
  41. render json: {
  42. success: true,
  43. }
  44. end
  45. private
  46. def cache
  47. UploadCache.new(params[:id])
  48. end
  49. end