summaryrefslogtreecommitdiffhomepage
path: root/app/controllers/api/v1/games_controller.rb
blob: 6a412c8266a4ab836a5f278360958a9f027be0df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
require "zip"

class Api::V1::GamesController < ApplicationController
  #skip_before_action :verify_authenticity_token
  before_action :allow_iframe, only: [:show_file]
  def create
    puts "----- PARAMS PLATFORM TAG ----------"
    pp params["game"]["platform_tag"]
    user = User.find_by(access_token_digest: cookies[:session])
    #user = User.first # temporary for debug
    if(!user)
      render json: {session: cookies[:session]}, status: 401
    else
      pp params

      @game = user.games.new(game_params.except(:status, :platform_tag))
      @game.titleSlug = game_params[:title].parameterize
      @game.status = game_params[:status].to_i
      if !params["game"]["platform_tag"].nil?
        params["game"]["platform_tag"].each do |tag|
          tag_obj = Tag.find_by(tag_type: "platform", name: tag)
          if tag_obj
            @game.tags << tag_obj
          end
        end
      end

      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('.')

            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('/')

              # saving the game wont have the blob saved so we need to do it manually
              @game.game_files.last.blob.save 
            end

          end
        end
      end

      if @game.save
        render json: @game, status: :created
      else
        render json: @game.errors, status: :unprocessable_entity
      end
    end
  end


  # list of all games
  def index
    game = Game.all.order(created_at: :desc)
    #render json: game
    render json: game.to_json(include: [:game_files, :card_img, :char_img, :title_img, :tags])
  end

  # single game or list of user's games
  #get 'games/:user/:game', to: 'games#show'
  #get 'games/:user', to: 'games#show'
  def show
    user = User.find_by! user_name: params[:user]
    if params[:game].nil?
      # get list of user games
      games = Game.where(user_id: user.id).order(created_at: :desc)
      render json: games.to_json(include: [:tags])
    else
      game = Game.find_by! user_id: user.id, titleSlug: params[:game]
      render json: game.to_json(include: [:tags])
      # get game
    end
  end

  # :user/:game/*path/:file
  def show_file
    user = User.find_by user_name: params[:user]

    # if no user given then just show all games
    if(user.nil?)
      game = Game.all.order(created_at: :desc)
      render json: game
      return
    end

    game = Game.find_by user_id: user.id, titleSlug: params[:game]

    # if no game given then just show all games from that user
    if(game.nil?)
      game = Game.all.order(created_at: :desc)
      render json: game
      return
    end

    # format and file is seperated in rails
    filename = params[:file]
    if !params[:format].nil?
      filename = "#{filename}.#{params[:format]}"
    end

    # if we have no path, make it a blank string
    # this lets us later match with files that are in the root
    params[:path] ||= ""

    result = game.game_files.blobs.find_by(filename: filename, filepath: params[:path].delete_suffix('/').delete_prefix('/')) # TODO check if we need to do the prefix/suffix deletion at all

    # we shouldnt need this
    #result ||= game.game_files.blobs.find_by(filename: filename)
    if(result.nil?)
      game = Game.all.order(created_at: :desc)
      render json: { filename: filename, filepath: params[:path] }
      #render json: game
      return
    end

    format = filename.rpartition('.').last
    if format == "html"
      render html: result.download.html_safe
    elsif format == "js"
      render js: result.download.html_safe
      #else
      #  redirect_to url_for(result)
      #end
    elsif format == "gz"
      response.headers['Content-Encoding'] = 'gzip'
      second_ext = filename.rpartition('.').first.rpartition('.').last
      if second_ext == 'js'
        send_data result.download.html_safe, filename: filename, disposition: "inline", type: "application/javascript"
      elsif second_ext == 'wasm'
        send_data result.download.html_safe, filename: filename, disposition: "inline", type: "application/wasm"
      elsif second_ext == 'data'
        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
      send_data result.download.html_safe, filename: filename, disposition: "inline"
    end
  end

  #get 'imggames/:user/:game?type=___', to: 'games#show_img'
  def show_img
    user = User.find_by! user_name: params[:user]
    game = Game.find_by! user_id: user.id, titleSlug: params[:game]

    result = nil;
    if params[:type] == "char"
      result = game.char_img.download
    elsif params[:type] == "title"
      result = game.title_img.download
    elsif params[:type] == "card"
      result = game.card_img.download
    end

    send_data result, type: 'image/png', disposition: 'inline'
  end

  private

  def game_params 
    params.require(:game).permit(
      :title,
      :description,
      :github_link,
      :img_rendering,
      :status,
      :order,
      :card_img,
      :char_img,
      :title_img,
      :zip,
      :platform_tag
      #game_files: []
    )
  end

  def allow_iframe
    response.headers.delete('X-Frame-Options')
  end
end