Wednesday 15 February 2012

ruby on rails - RoR: Handling Blanks AND/OR Special Characters -



ruby on rails - RoR: Handling Blanks AND/OR Special Characters -

i'm processing emails upload , embedded image in email comes through either without file extension or extension containing random combination of letters, numbers , special characters (for example: image001.gif@01cfa02b.47556390). if either instance arrives, want ignore , move on. think i've got without extension covered, wasn't clear on how best handle random characters , cleanest way in write conditionals. here have far:

filename_extension = file.extname(filename) if filename_extension.blank? puts "filename ext blank" elsif filename_extension #need regex or handle random? puts "filename ext random" else #do processing

thanks.

known_extensions = %w[.csv .rb .rbw .html .htm .css] filenames = %w[1.txt 2.csv 3] filenames.each |filename| filename_extension = file.extname(filename) if filename_extension.empty? puts "filename ext blank" elsif known_extensions.include? filename_extension puts "filename ext random" else #do processing puts "processing" end end

the question tagged ruby without indicate of gems may give blank? method.

the thought of 'invalid' extension rather varied, , of course of study tied means valid file name. on unix file systems, example, limitations on file name limitation of filename size of 255 bytes, , reserved characters of / , null. in fact, there no specification aware of 'extensions' in unix, part of file name, period in file name beingness valid, not signifying special. (with exception of file name starts period indicating should 'hidden' file.) on windows system, longer list of characters, of / : < > ? \ | + , . ; = [ ] single , double quotes. on commodore, think :, , , =, , on amiga scheme utilize except ", :, or /.

so think 'invalid' extension might easier match 'valid' ones. if indeed using rails, , hosting on unix, have smaller set of things check for, ensure valid extension (indeed, valid filename). basing invalid extension on hosting system, , restrictions place due thought of valid extension means program.

ruby-on-rails ruby regex

No comments:

Post a Comment