diff options
| author | Tradam <[email protected]> | 2024-05-29 19:26:06 -0400 |
|---|---|---|
| committer | Tradam <[email protected]> | 2024-05-29 19:26:06 -0400 |
| commit | 22c1d8d71de2bd52513f684196fa66a53e7362aa (patch) | |
| tree | 783537b87d8e8523b319a4fc5ea441ca55a0eb8c | |
| parent | fd4edcfac9041cadfc4c88257555c02736bc4ba4 (diff) | |
| download | gameHolster-22c1d8d71de2bd52513f684196fa66a53e7362aa.tar.gz gameHolster-22c1d8d71de2bd52513f684196fa66a53e7362aa.zip | |
switch over to serving games through zip and make unity games work
| -rw-r--r-- | Gemfile | 2 | ||||
| -rw-r--r-- | Gemfile.lock | 2 | ||||
| -rw-r--r-- | app/controllers/api/v1/games_controller.rb | 83 | ||||
| -rw-r--r-- | app/models/game.rb | 2 | ||||
| -rw-r--r-- | config/application.rb | 2 | ||||
| -rw-r--r-- | config/routes.rb | 2 | ||||
| -rw-r--r-- | db/migrate/20240521205000_create_games.rb | 5 | ||||
| -rw-r--r-- | db/migrate/20240527231908_add_path_to_active_storage_blobs.rb | 5 | ||||
| -rw-r--r-- | db/schema.rb | 3 |
9 files changed, 91 insertions, 15 deletions
@@ -45,3 +45,5 @@ group :development do # gem "spring" end + +gem "rubyzip", "~> 2.3" diff --git a/Gemfile.lock b/Gemfile.lock index 15a8740..f163f71 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -183,6 +183,7 @@ GEM psych (>= 4.0.0) reline (0.5.7) io-console (~> 0.5) + rubyzip (2.3.2) stringio (3.1.0) thor (1.3.1) timeout (0.4.1) @@ -210,6 +211,7 @@ DEPENDENCIES puma (>= 5.0) rack-cors rails (~> 7.1.3, >= 7.1.3.3) + rubyzip (~> 2.3) tzinfo-data RUBY VERSION diff --git a/app/controllers/api/v1/games_controller.rb b/app/controllers/api/v1/games_controller.rb index 5a4fca0..8a44d89 100644 --- a/app/controllers/api/v1/games_controller.rb +++ b/app/controllers/api/v1/games_controller.rb @@ -1,20 +1,51 @@ +require "zip" + + class Api::V1::GamesController < ApplicationController #skip_before_action :verify_authenticity_token before_action :allow_iframe, only: [:play] def create user = User.find_by(access_token_digest: cookies[:session]) - user = User.first + user = User.first # temporary for debug if(!user) render json: {}, status: 401 else pp params - + @game = user.games.new(game_params)#Game.new(game_params) @game.titleSlug = game_params[:title].parameterize - #@game.user_id = user.id - #user.games << @game - if @game.save + pp params + + Zip::File.open(params[:game][:zip]) do |zipfile| + zipfile.each do |entry| + if entry.file? + path_name = entry.name.rpartition('/') + name_extension = path_name.last.rpartition('.') + #puts " --- " + #puts "Found file: #{entry.name.rpartition('/').last} at #{entry.name.rpartition('/').first}" + #puts " --- " + Tempfile.open([name_extension.first, name_extension[1] + name_extension.last]) do |temp_file| + entry.extract(temp_file.path) { true } + @game.game_files.attach(io: File.open(temp_file.path), filename: path_name.last); + @game.game_files.last.blob.filepath = path_name.first.delete_suffix('/').delete_prefix('/') + @game.game_files.last.blob.save + @game.save + #@game.game_files.last.path = path_name.first + puts "" + puts "" + puts " GAME FILES" + pp @game.game_files.size + pp @game.game_files.last + puts "" + puts "" + #activerecord_file.save + end + end + end + end + + if @game.save render json: @game, status: :created else render json: @game.errors, status: :unprocessable_entity @@ -67,20 +98,48 @@ class Api::V1::GamesController < ApplicationController filename = "#{filename}.#{params[:format]}" end - result = game.game_files.blobs.find_by(filename: filename) + puts + puts "HERE" + puts params[:path] + puts + params[:path] ||= "" + result = game.game_files.blobs.find_by(filename: filename, filepath: params[:path].delete_suffix('/').delete_prefix('/')) + #result = game.game_files.blobs.find_by(filename: filename, filepath: params[:path]) + + result ||= game.game_files.blobs.find_by(filename: filename) if(result.nil?) game = Game.all.order(created_at: :desc) - render json: game + render json: { filename: filename, filepath: params[:path] } + #render json: game return end - if params[:format] == "html" + format = filename.rpartition('.').last + + if format == "html" render html: result.download.html_safe - elsif params[:format] == "js" + elsif format == "js" render js: result.download.html_safe + #else + # redirect_to url_for(result) + #end + elsif format == "gz" + second_ext = filename.rpartition('.').first.rpartition('.').last + if second_ext == 'js' + response.headers['Content-Encoding'] = 'gzip' + send_data result.download.html_safe, filename: filename, disposition: "inline", type: "application/javascript" + elsif second_ext == 'wasm' + response.headers['Content-Encoding'] = 'gzip' + send_data result.download.html_safe, filename: filename, disposition: "inline", type: "application/wasm" + elsif second_ext == 'data' + response.headers['Content-Encoding'] = 'gzip' + send_data result.download.html_safe, filename: filename, disposition: "inline", type: "application/octet-stream" + else + send_data result.download.html_safe, filename: filename, disposition: "inline" + end else - render plain: result.download + send_data result.download.html_safe, filename: filename, disposition: "inline" end #render html: game.game_files.first.download.html_safe #Game.first.game_file.download.html_safe @@ -113,7 +172,9 @@ class Api::V1::GamesController < ApplicationController :card_img, :char_img, :title_img, - game_files: []) + :zip + #game_files: [] + ) end def allow_iframe diff --git a/app/models/game.rb b/app/models/game.rb index 4bba6f7..b4b98b1 100644 --- a/app/models/game.rb +++ b/app/models/game.rb @@ -1,10 +1,12 @@ class Game < ApplicationRecord + enum status: { draft: 0, published: 1 } #enum status: { # draft: 0, # published: 1 #} belongs_to :user has_many_attached :game_files + has_one_attached :zip has_one_attached :card_img has_one_attached :char_img has_one_attached :title_img diff --git a/config/application.rb b/config/application.rb index 60b2740..313b2a2 100644 --- a/config/application.rb +++ b/config/application.rb @@ -31,6 +31,8 @@ module GamesHost config.middleware.use ActionDispatch::Cookies config.middleware.use ActionDispatch::Session::CookieStore + + config.active_storage.content_types_allowed_inline << "text/html" #localhost:5173 #config.session_store :cookie_store, key: 'session', domain: :all, tld_length: 5 diff --git a/config/routes.rb b/config/routes.rb index adbfb07..6f86bb7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,7 @@ Rails.application.routes.draw do # isolated domain, do not allow auth here #constraints host: 'localhost' do # GAMES - get 'game/:user/:game/*path/:file', to: 'api/v1/games#play' + get 'game/:user/:game/*path/:file', to: 'api/v1/games#play', constraints: { file: /[^\/]+/ } get 'game/:user/:game/:file', to: 'api/v1/games#play' #end diff --git a/db/migrate/20240521205000_create_games.rb b/db/migrate/20240521205000_create_games.rb index 236c070..d65b118 100644 --- a/db/migrate/20240521205000_create_games.rb +++ b/db/migrate/20240521205000_create_games.rb @@ -4,9 +4,10 @@ class CreateGames < ActiveRecord::Migration[7.1] t.string :title t.string :titleSlug t.string :description + t.string :github_link t.string :img_rendering - #t.integer :status, default: 0 - #t.integer :order, default: 0 + t.integer :status, default: 0 + t.integer :order, default: 0 t.timestamps end diff --git a/db/migrate/20240527231908_add_path_to_active_storage_blobs.rb b/db/migrate/20240527231908_add_path_to_active_storage_blobs.rb new file mode 100644 index 0000000..c591ae1 --- /dev/null +++ b/db/migrate/20240527231908_add_path_to_active_storage_blobs.rb @@ -0,0 +1,5 @@ +class AddPathToActiveStorageBlobs < ActiveRecord::Migration[7.1] + def change + add_column :active_storage_blobs, :filepath, :string, null: false, default: '' + end +end diff --git a/db/schema.rb b/db/schema.rb index cee805e..58a01aa 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_05_21_205000) do +ActiveRecord::Schema[7.1].define(version: 2024_05_27_231908) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -33,6 +33,7 @@ ActiveRecord::Schema[7.1].define(version: 2024_05_21_205000) do t.bigint "byte_size", null: false t.string "checksum" t.datetime "created_at", null: false + t.string "filepath", default: "", null: false t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true end |
