Skip to content

Commit

Permalink
Support Crystal 0.35.0
Browse files Browse the repository at this point in the history
  • Loading branch information
omarroth committed Jun 15, 2020
1 parent 338dc32 commit d30a972
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 22 deletions.
6 changes: 3 additions & 3 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ targets:
dependencies:
pg:
github: will/crystal-pg
version: ~> 0.21.0
version: ~> 0.21.1
sqlite3:
github: crystal-lang/crystal-sqlite3
version: ~> 0.16.0
kemal:
github: kemalcr/kemal
version: ~> 0.26.1
branch: master
pool:
github: ysbaddaden/pool
version: ~> 0.2.3
Expand All @@ -28,6 +28,6 @@ dependencies:
github: omarroth/lsquic.cr
branch: dev

crystal: 0.34.0
crystal: 0.35.0

license: AGPLv3
4 changes: 2 additions & 2 deletions src/invidious.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require "pg"
require "sqlite3"
require "xml"
require "yaml"
require "zip"
require "compress/zip"
require "protodec/utils"
require "./invidious/helpers/*"
require "./invidious/*"
Expand Down Expand Up @@ -2660,7 +2660,7 @@ post "/data_control" do |env|

PG_DB.exec("UPDATE users SET feed_needs_update = true, subscriptions = $1 WHERE email = $2", user.subscriptions, user.email)
when "import_newpipe"
Zip::Reader.open(IO::Memory.new(body)) do |file|
Compress::Zip::Reader.open(IO::Memory.new(body)) do |file|
file.each_entry do |entry|
if entry.filename == "newpipe.db"
tempfile = File.tempfile(".db")
Expand Down
4 changes: 2 additions & 2 deletions src/invidious/helpers/handlers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ class FilteredCompressHandler < Kemal::Handler

if request_headers.includes_word?("Accept-Encoding", "gzip")
env.response.headers["Content-Encoding"] = "gzip"
env.response.output = Gzip::Writer.new(env.response.output, sync_close: true)
env.response.output = Compress::Gzip::Writer.new(env.response.output, sync_close: true)
elsif request_headers.includes_word?("Accept-Encoding", "deflate")
env.response.headers["Content-Encoding"] = "deflate"
env.response.output = Flate::Writer.new(env.response.output, sync_close: true)
env.response.output = Compress::Deflate::Writer.new(env.response.output, sync_close: true)
end

call_next env
Expand Down
50 changes: 38 additions & 12 deletions src/invidious/helpers/helpers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ def extract_shelf_items(nodeset, ucid = nil, author_name = nil)
end

def check_enum(db, logger, enum_name, struct_type = nil)
return # TODO
if !db.query_one?("SELECT true FROM pg_type WHERE typname = $1", enum_name, as: Bool)
logger.puts("CREATE TYPE #{enum_name}")

Expand All @@ -646,18 +647,14 @@ def check_table(db, logger, table_name, struct_type = nil)
end
end

if !struct_type
return
end
return if !struct_type

struct_array = struct_type.to_type_tuple
column_array = get_column_array(db, table_name)
column_types = File.read("config/sql/#{table_name}.sql").match(/CREATE TABLE public\.#{table_name}\n\((?<types>[\d\D]*?)\);/)
.try &.["types"].split(",").map { |line| line.strip }
.try &.["types"].split(",").map { |line| line.strip }.reject &.starts_with?("CONSTRAINT")

if !column_types
return
end
return if !column_types

struct_array.each_with_index do |name, i|
if name != column_array[i]?
Expand Down Expand Up @@ -708,6 +705,15 @@ def check_table(db, logger, table_name, struct_type = nil)
end
end
end

return if column_array.size <= struct_array.size

# column_array.each do |column|
# if !struct_array.includes? column
# logger.puts("ALTER TABLE #{table_name} DROP COLUMN #{column} CASCADE")
# db.exec("ALTER TABLE #{table_name} DROP COLUMN #{column} CASCADE")
# end
# end
end

class PG::ResultSet
Expand Down Expand Up @@ -897,15 +903,35 @@ end

def proxy_file(response, env)
if response.headers.includes_word?("Content-Encoding", "gzip")
Gzip::Writer.open(env.response) do |deflate|
response.pipe(deflate)
Compress::Gzip::Writer.open(env.response) do |deflate|
IO.copy response.body_io, deflate
end
elsif response.headers.includes_word?("Content-Encoding", "deflate")
Flate::Writer.open(env.response) do |deflate|
response.pipe(deflate)
Compress::Deflate::Writer.open(env.response) do |deflate|
IO.copy response.body_io, deflate
end
else
response.pipe(env.response)
IO.copy response.body_io, env.response
end
end

# See https://github.com/kemalcr/kemal/pull/576
class HTTP::Server::Response::Output
def close
return if closed?

unless response.wrote_headers?
response.content_length = @out_count
end

ensure_headers_written

super

if @chunked
@io << "0\r\n\r\n"
@io.flush
end
end
end

Expand Down
4 changes: 2 additions & 2 deletions src/invidious/helpers/static_file_handler.cr
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ def send_file(env : HTTP::Server::Context, file_path : String, data : Slice(UInt
condition = config.is_a?(Hash) && config["gzip"]? == true && filesize > minsize && Kemal::Utils.zip_types(file_path)
if condition && request_headers.includes_word?("Accept-Encoding", "gzip")
env.response.headers["Content-Encoding"] = "gzip"
Gzip::Writer.open(env.response) do |deflate|
Compress::Gzip::Writer.open(env.response) do |deflate|
IO.copy(file, deflate)
end
elsif condition && request_headers.includes_word?("Accept-Encoding", "deflate")
env.response.headers["Content-Encoding"] = "deflate"
Flate::Writer.open(env.response) do |deflate|
Compress::Deflate::Writer.open(env.response) do |deflate|
IO.copy(file, deflate)
end
else
Expand Down
2 changes: 1 addition & 1 deletion src/invidious/helpers/utils.cr
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ end
def sha256(text)
digest = OpenSSL::Digest.new("SHA256")
digest << text
return digest.hexdigest
return digest.final.hexstring
end

def subscribe_pubsub(topic, key, config)
Expand Down

0 comments on commit d30a972

Please sign in to comment.