Compare commits

...

4 Commits

Author SHA1 Message Date
ChunkyProgrammer
d9fcfe6a23
Merge 77de46f316 into 4782a67038 2024-08-27 13:58:21 -04:00
ChunkyProgrammer
77de46f316 Use a find instead of an each loop 2024-08-24 18:02:55 -04:00
ChunkyProgrammer
123c4dfba5 Put temp.delete inside ensure block 2024-08-24 18:02:55 -04:00
ChunkyProgrammer
a0bb4b32c6 Ensure IO is properly closed when importing NewPipe subscriptions 2024-08-24 18:02:55 -04:00

View File

@ -290,26 +290,22 @@ struct Invidious::User
end
def from_newpipe(user : User, body : String) : Bool
io = IO::Memory.new(body)
Compress::Zip::File.open(io) do |file|
file.entries.each do |entry|
Compress::Zip::File.open(IO::Memory.new(body), true) do |file|
entry = file.entries.find { |file_entry| file_entry.filename == "newpipe.db" }
return false if entry.nil?
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
temp = File.tempfile(".db") do |tempfile|
begin
File.write(tempfile.path, io_sized.gets_to_end)
rescue
return false
end
db = DB.open("sqlite3://" + tempfile.path)
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="))
@ -323,9 +319,10 @@ struct Invidious::User
user.subscriptions = get_batch_channels(user.subscriptions)
Invidious::Database::Users.update_subscriptions(user)
db.close
tempfile.delete
end
end
ensure
temp.delete if !temp.nil?
end
end
end