ぼざクリタグ広場 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.
 
 
 
 
 
 

40 lines
1012 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: WikiAssetRepr.many(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. asset = nil
  18. page.with_lock do
  19. no = page.next_asset_no
  20. alt_text = params[:alt_text].presence
  21. sha256 = Digest::SHA256.file(file.tempfile.path).digest
  22. asset = WikiAsset.new(wiki_page_id:, no:, alt_text:, sha256:, created_by_user: current_user)
  23. asset.file.attach(file)
  24. asset.save!
  25. page.update!(next_asset_no: no + 1)
  26. end
  27. render json: WikiAssetRepr.base(asset)
  28. end
  29. end