s3_uploads_controller.rb 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. class Admin::S3UploadsController < Admin::ApplicationController
  2. # GET /admin/source_files
  3. # GET /admin/source_files.json
  4. def index
  5. if params[:f].present?
  6. source_files = SourceFile.where("LOWER(file_name) LIKE ?", "%#{params[:f]}%".downcase)
  7. else
  8. source_files = SourceFile.limit(3)
  9. end
  10. source_files = source_files.order(created_at: :desc)
  11. respond_to do |format|
  12. format.html { render partial: "table", locals: { source_files: source_files } }
  13. end
  14. end
  15. # POST /admin/source_files
  16. # POST /admin/source_files.json
  17. def create
  18. # this line allows for compatibility with `ProtectedAttributes` or `StrongParameters`
  19. parameters = S3CorsFileupload.active_record_protected_attributes? ? params[:source_file] : params.require(:source_file).permit(:url, :bucket, :key)
  20. @source_file = SourceFile.new(parameters)
  21. respond_to do |format|
  22. if @source_file.save
  23. format.html {
  24. render json: @source_file.to_jq_upload,
  25. content_type: "text/html",
  26. layout: false
  27. }
  28. format.json { render json: @source_file.to_jq_upload, status: :created }
  29. else
  30. format.html { render "new" }
  31. format.json { render json: @source_file.errors, status: :unprocessable_entity }
  32. end
  33. end
  34. end
  35. # DELETE /admin/source_files/1
  36. # DELETE /admin/source_files/1.json
  37. def destroy
  38. @source_file = SourceFile.find(params[:id])
  39. @source_file.destroy
  40. respond_to do |format|
  41. format.html { redirect_to source_files_url }
  42. format.json { head :no_content }
  43. format.xml { head :no_content }
  44. end
  45. end
  46. # used for s3_uploader on the javascript of the upload to gallery section
  47. # for /admin/action_page/new
  48. # GET /admin/source_files/generate_key
  49. def generate_key
  50. uid = SecureRandom.uuid.gsub(/-/, "")
  51. render json: {
  52. key: "uploads/#{uid}/#{params[:filename]}",
  53. success_action_redirect: "/"
  54. }
  55. end
  56. end