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