ぼざクリタグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

39 lines
956 B

  1. require 'digest'
  2. class WikiAssetsController < ApplicationController
  3. def index
  4. page_id = params[:wiki_page_id].to_i
  5. page = WikiPage.find_by(id: page_id)
  6. return head :not_found unless page
  7. render json: page.assets
  8. end
  9. def create
  10. return head :unauthorized unless current_user
  11. return head :forbidden unless current_user.gte_member?
  12. wiki_page_id = params[:wiki_page_id].to_i
  13. page = WikiPage.find_by(id: wiki_page_id)
  14. return head :not_found unless page
  15. file = params[:file]
  16. return head :bad_request if file.blank?
  17. page.with_lock do
  18. no = page.next_asset_no
  19. alt_text = params[:alt_text].presence
  20. sha256 = Digest::SHA256.file(file.tempfile.path).digest
  21. asset = WikiAsset.new(wiki_page_id:, no:, alt_text:, sha256:, created_by_user: current_user)
  22. asset.file.attach(file)
  23. asset.save!
  24. page.update!(next_asset_no: no + 1)
  25. end
  26. render json: asset
  27. end
  28. end