diff options
| author | realtradam <[email protected]> | 2024-05-16 22:10:46 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2024-05-16 22:10:46 -0400 |
| commit | 98571b599449bfd7497c9b098a07b376da670319 (patch) | |
| tree | 7fc85863cd7d8406dc0c537dd893111f662d8938 | |
| parent | 43237d776e311ebd00b36c0048ec339a4da5b15b (diff) | |
| download | gameHolster-98571b599449bfd7497c9b098a07b376da670319.tar.gz gameHolster-98571b599449bfd7497c9b098a07b376da670319.zip | |
implement uploading and serving web games
| -rw-r--r-- | app/controllers/api/v1/games_controller.rb | 25 | ||||
| -rw-r--r-- | app/javascript/components/Games.jsx | 13 | ||||
| -rw-r--r-- | 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 () { <input type="text" name="title" /> </div> <div> - <label>File</label> - <input type="file" name="game_file" /> + <label>Files</label> + <input type="file" multiple="multiple" name="game_files" /> </div> <button type="submit" className="w-32 bg-stone-900 text-stone-50 rounded">submit</button> </form> 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 |
