server: return error when attachment download failed

This commit is contained in:
King Kévin 2023-03-26 15:39:53 +02:00
parent 3ffaf22368
commit 6d2b15329f
1 changed files with 5 additions and 3 deletions

View File

@ -460,11 +460,11 @@ get '/import/lcsc/:lcsc' do
end end
get '/attach?' do get '/attach?' do
halt 401, "part name or id required" unless params['id'] or params['name'] halt 400, "part name or id required" unless params['id'] or params['name']
halt 401, "attachement URL required" unless params['url'] halt 400, "attachement URL required" unless params['url']
statement = @db.prepare("SELECT id, name FROM part WHERE id = ? OR name = ?") statement = @db.prepare("SELECT id, name FROM part WHERE id = ? OR name = ?")
part = statement.execute(params['id'], params['name']).to_a[0] part = statement.execute(params['id'], params['name']).to_a[0]
halt 401, "unknown part" unless part halt 400, "unknown part" unless part
file = CGI.unescape(params['url']).split("/")[-1] file = CGI.unescape(params['url']).split("/")[-1]
dir = PUBLIC + "/" + ATTACHMENTS + "/" + part["name"].gsub("/", "_") dir = PUBLIC + "/" + ATTACHMENTS + "/" + part["name"].gsub("/", "_")
path = "#{dir}/#{file}" path = "#{dir}/#{file}"
@ -476,6 +476,8 @@ get '/attach?' do
File.open(path, "wb") do |f| File.open(path, "wb") do |f|
f.write res.body f.write res.body
end end
else
halt 404, "download failed"
end end
end end
end end