server: fix LCSC import attachment download
This commit is contained in:
parent
e58aefc3c5
commit
71aa43faf0
10
server.rb
10
server.rb
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue