server: fix LCSC import attachment download

This commit is contained in:
King Kévin 2023-01-30 00:38:28 +01:00
parent e58aefc3c5
commit 71aa43faf0
1 changed files with 5 additions and 5 deletions

View File

@ -388,20 +388,20 @@ get '/import/lcsc/:lcsc' do
part["datasheet"] = result["pdfUrl"]
existing = get_part_by_name(part["name"])
halt 401, "part name already exists" if existing
puts part
begin
add_part(part)
rescue StandardError => e
halt 401, e.message
end
i = 0
(part["attachments"] + [part["datasheet"]]).each do |attachement|
file = attachement.split("/")[-1]
(part["attachments"] + [part["datasheet"]]).each do |attachment|
next unless attachment
file = attachment.split("/")[-1]
dir = PUBLIC + "/" + ATTACHMENTS + "/" + part["name"]
path = "#{dir}/#{i}_#{file}"
i += 1
unless File.file?(path) then
uri = URI(attachement)
uri = URI(attachment)
res = Net::HTTP.get_response(uri)
if (res.is_a?(Net::HTTPSuccess)) then
Dir.mkdir(dir) unless File.directory?(dir)
@ -411,5 +411,5 @@ get '/import/lcsc/:lcsc' do
end
end
end
return 200
return 200, "#{part['name']} added"
end