From 98571b599449bfd7497c9b098a07b376da670319 Mon Sep 17 00:00:00 2001 From: realtradam Date: Thu, 16 May 2024 22:10:46 -0400 Subject: implement uploading and serving web games --- app/controllers/api/v1/games_controller.rb | 25 +++++++++++++++++++++++-- app/javascript/components/Games.jsx | 13 ++++++++----- app/models/game.rb | 2 +- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/app/controllers/api/v1/games_controller.rb b/app/controllers/api/v1/games_controller.rb index 67bc947..7b033df 100644 --- a/app/controllers/api/v1/games_controller.rb +++ b/app/controllers/api/v1/games_controller.rb @@ -33,12 +33,33 @@ class Api::V1::GamesController < ApplicationController return end - render html: game.game_file.download.html_safe #Game.first.game_file.download.html_safe + filename = params[:file] + if !params[:format].nil? + filename = "#{filename}.#{params[:format]}" + end + + result = game.game_files.blobs.find_by(filename: filename) + + if(result.nil?) + game = Game.all.order(created_at: :desc) + render json: game + return + end + + if params[:format] == "html" + render html: result.download.html_safe + elsif params[:format] == "js" + render js: result.download.html_safe + else + render plain: result.download + end + + #render html: game.game_files.first.download.html_safe #Game.first.game_file.download.html_safe end private def games_params - params.require(:game).permit(:title, :game_file, :titleSlug) + params.require(:game).permit(:title, :titleSlug, game_files: []) end end diff --git a/app/javascript/components/Games.jsx b/app/javascript/components/Games.jsx index adf9f2d..1862caa 100644 --- a/app/javascript/components/Games.jsx +++ b/app/javascript/components/Games.jsx @@ -19,10 +19,13 @@ export default function Games () { )); var handleSubmit = (e) => { e.preventDefault() //stops submit from happening - + const form = e.target; const formData = new FormData() - formData.append('game[title]', e.target.title.value) - formData.append('game[game_file]', e.target.game_file.files[0], e.target.game_file.value) + formData.append('game[title]', form.title.value) + for(let i =0; i < form.game_files.files.length; i++) + { + formData.append('game[game_files][]', form.game_files.files[i], form.game_files.files[i].value); + } for (var pair of formData.entries()) { console.log(pair[0] + ', ' + pair[1]) @@ -47,8 +50,8 @@ export default function Games () {
- - + +
diff --git a/app/models/game.rb b/app/models/game.rb index fbdc561..07c5098 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -4,5 +4,5 @@ class Game < ApplicationRecord # published: 1 #} #belongs_to :user - has_one_attached :game_file + has_many_attached :game_files end -- cgit v1.2.3