अतीत में, मुझे कमांड लाइन उपकरण "wget" का उपयोग करके दूरस्थ फ़ाइलों को पुनर्प्राप्त करने का सबसे विश्वसनीय तरीका मिला। निम्नलिखित कोड ज्यादातर एक मौजूदा उत्पादन से सीधे कॉपी किया जाता है कुछ बदलाव के साथ एप्लिकेशन (2.x रेल) अपने कोड उदाहरण के साथ फिट करने के लिए:
[
['Cat', 'cat'],
['Dog', 'dog'],
].each do |name, icon|
CategoryIconImporter.import_from_url {:name => name}, "https://xyz.com/images/#{icon}.png"
end
:
class CategoryIconImporter
def self.download_to_tempfile (url)
system(wget_download_command_for(url))
@@tempfile.path
end
def self.clear_tempfile
@@tempfile.delete if @@tempfile && @@tempfile.path && File.exist?(@@tempfile.path)
@@tempfile = nil
end
def self.set_wget
# used for retrieval in NrlImage (and in future from other sies?)
if [email protected]@wget
stdin, stdout, stderr = Open3.popen3('which wget')
@@wget = stdout.gets
@@wget ||= '/usr/local/bin/wget'
@@wget.strip!
end
end
def self.wget_download_command_for (url)
set_wget
@@tempfile = Tempfile.new url.sub(/\?.+$/, '').split(/[\/\\]/).last
command = [ @@wget ]
command << '-q'
if url =~ /^https/
command << '--secure-protocol=auto'
command << '--no-check-certificate'
end
command << '-O'
command << @@tempfile.path
command << url
command.join(' ')
end
def self.import_from_url (category_params, url)
clear_tempfile
filename = url.sub(/\?.+$/, '').split(/[\/\\]/).last
found = MIME::Types.type_for(filename)
content_type = !found.empty? ? found.first.content_type : nil
download_to_tempfile url
nicer_path = RAILS_ROOT + '/tmp/' + filename
File.copy @@tempfile.path, nicer_path
Category.create(category_params.merge({:icon => ActionController::TestUploadedFile.new(nicer_path, content_type, true)}))
end
end
रेक कार्य तर्क कैसा लग सकता है
यह सामग्री प्रकार खोज के लिए माइम-प्रकार मणि का उपयोग करता है:
gem 'mime-types', :require => 'mime/types'
स्रोत
2011-06-15 02:25:20
आप किसी अन्य साइट से छवियों को पुनः प्राप्त करने,, हाँ कोशिश कर रहे हैं समस्या के कुछ प्रकार हो रही है? तब पेपरक्लिप सामान क्यों? आप सिर्फ आइकन डाउनलोड क्यों नहीं करते हैं और उन्हें अलग से अपलोड करते हैं? आपको एहसास है कि फ़ाइल अपलोड को संभालने के लिए पेपरक्लिप, है ना? – coreyward
मैं पेपरक्लिप शैली में आइकन जोड़ना चाहता हूं। मेरे पास 400 फ़ोल्डर हैं जो मेरे लिए कई फ़ोल्डर्स बनाने के लिए संभव नहीं हैं। –
इन आइकनों पर @coreyward अधिक कुछ श्रेणी से संबंधित हैं और प्रत्येक श्रेणी में कई अन्य विवरण हैं। –