Skip to content

Commit

Permalink
Ensure IO is properly closed when importing NewPipe subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunkyProgrammer committed Dec 20, 2023
1 parent 97c4165 commit 8c15384
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions src/invidious/user/imports.cr
Expand Up @@ -290,42 +290,38 @@ struct Invidious::User
end

def from_newpipe(user : User, body : String) : Bool
io = IO::Memory.new(body)

Compress::Zip::File.open(io) do |file|
Compress::Zip::File.open(IO::Memory.new(body), true) do |file|
file.entries.each do |entry|
entry.open do |file_io|
# Ensure max size of 4MB
io_sized = IO::Sized.new(file_io, 0x400000)

next if entry.filename != "newpipe.db"

tempfile = File.tempfile(".db")

begin
File.write(tempfile.path, io_sized.gets_to_end)
rescue
return false
end

db = DB.open("sqlite3://" + tempfile.path)
# Ensure max size of 4MB
io_sized = IO::Sized.new(file_io, 0x400000)

user.watched += db.query_all("SELECT url FROM streams", as: String)
.map(&.lchop("https://www.youtube.com/watch?v="))
temp = File.tempfile(".db") do |tempfile|
begin
File.write(tempfile.path, io_sized.gets_to_end)
rescue
return false
end

user.watched.uniq!
Invidious::Database::Users.update_watch_history(user)
DB.open("sqlite3://" + tempfile.path) do |db|
user.watched += db.query_all("SELECT url FROM streams", as: String)
.map(&.lchop("https://www.youtube.com/watch?v="))

user.subscriptions += db.query_all("SELECT url FROM subscriptions", as: String)
.map(&.lchop("https://www.youtube.com/channel/"))
user.watched.uniq!
Invidious::Database::Users.update_watch_history(user)

user.subscriptions.uniq!
user.subscriptions = get_batch_channels(user.subscriptions)
user.subscriptions += db.query_all("SELECT url FROM subscriptions", as: String)
.map(&.lchop("https://www.youtube.com/channel/"))

Invidious::Database::Users.update_subscriptions(user)
user.subscriptions.uniq!
user.subscriptions = get_batch_channels(user.subscriptions)

db.close
tempfile.delete
Invidious::Database::Users.update_subscriptions(user)
end
end
temp.delete
end
end
end
Expand Down

0 comments on commit 8c15384

Please sign in to comment.