class Struggle::Tfile
Public Class Methods
crop(imagepath, x, y, w, h)
click to toggle source
# File lib/struggle/tfile.rb, line 128 def Tfile.crop(imagepath, x, y, w, h) img = Magick::Image.read(imagepath)[0] if w==0 || h==0 w=img.columns h=img.rows end newimg = img.crop(x, y, w, h) newimg.write(imagepath) end
file_format(filename)
click to toggle source
# File lib/struggle/tfile.rb, line 64 def Tfile.file_format(filename) /\.[^\.]+$/.match(filename)[0] end
filename_validata(file, rule)
click to toggle source
# File lib/struggle/tfile.rb, line 49 def Tfile.filename_validata(file, rule) !eval("/\.(#{rule})+$/").match(file.original_filename).blank? end
getname(filestream, filepath)
click to toggle source
# File lib/struggle/tfile.rb, line 53 def Tfile.getname(filestream, filepath) file_for = Tfile.file_format(filestream.original_filename) filename = Digest::SHA1.hexdigest(SecureRandom.urlsafe_base64.to_s)<<file_for file = filepath+filename while File.exist?(file) do filename = Digest::SHA1.hexdigest(SecureRandom.urlsafe_base64.to_s)<<file_for file = filepath+filename end filename end
imagecropupload(imgfile, filepath="", rule="jpg|bmp|gif|ico|pcx|jpeg|tif|png", minsize=0, maxsize=2000, x=0, y=0, w=0, h=0)
click to toggle source
# File lib/struggle/tfile.rb, line 105 def Tfile.imagecropupload(imgfile, filepath="", rule="jpg|bmp|gif|ico|pcx|jpeg|tif|png", minsize=0, maxsize=2000, x=0, y=0, w=0, h=0) result = Tfile.rule_validata(imgfile, rule, minsize, maxsize) if result[:state] sname = Tfile.getname(imgfile, filepath) begin unless Dir::exist?(filepath) unless system("mkdir -p #{filepath}") return {state: false, result: "目录创建失败,请于管理员联系"} end end File.open(filepath+sname, "wb") do |f| f.write(imgfile.read) end Tfile.crop(filepath + sname, x, y, w, h) return {state: true, result: sname} rescue return {state: false, result: "写入图片失败:#{$!}"} end else return {state: false, result: result[:message]} end end
imageupload(imgfile, filepath="", rule="jpg|bmp|gif|ico|pcx|jpeg|tif|png", minsize=0, maxsize=2000, w=0, h=0)
click to toggle source
# File lib/struggle/tfile.rb, line 68 def Tfile.imageupload(imgfile, filepath="", rule="jpg|bmp|gif|ico|pcx|jpeg|tif|png", minsize=0, maxsize=2000, w=0, h=0) result = Tfile.rule_validata(imgfile, rule, minsize, maxsize) if result[:state] sname = Tfile.getname(imgfile, filepath) begin unless Dir::exist?(filepath) unless system("mkdir -p #{filepath}") return {state: false, result: "目录创建失败,请于管理员联系"} end end File.open(filepath+sname, "wb") do |f| f.write(imgfile.read) end Tfile.resize(filepath + sname, w, h) return {state: true, result: sname} rescue return {state: false, result: "写入图片失败:#{$!}"} end else return {state: false, result: result[:message]} end end
resize(imagepath, w, h)
click to toggle source
# File lib/struggle/tfile.rb, line 95 def Tfile.resize(imagepath, w, h) img = Magick::Image.read(imagepath)[0] if w==0 || h==0 w=img.columns h=img.rows end newimg = img.resize_to_fill(w, h) newimg.write(imagepath) end
rmdir(dir)
click to toggle source
# File lib/struggle/tfile.rb, line 91 def Tfile.rmdir(dir) system("rm -rf #{dir}") end
rule_validata(file, rule, minsize, maxsize)
click to toggle source
# File lib/struggle/tfile.rb, line 38 def Tfile.rule_validata(file, rule, minsize, maxsize) rule_for = Tfile.filename_validata(file, rule) unless rule_for return {state: false, message: "错误:文件格式不对,只允许上传#{rule}格式!\\n"} end if file.size<minsize*1024 || file.size>maxsize*1024 return {state: false, message: "错误:文件大小错误,只允许#{minsize}kb~#{maxsize}kb!\\n"} end return {state: true} end
unpack(file, path)
click to toggle source
# File lib/struggle/tfile.rb, line 25 def Tfile.unpack(file, path) format = Tfile.file_format(file) cmd = "" if format==".gz" || format==".bz2" || format==".tar" cmd="tar -zxvf #{file} -C #{path}" elsif format==".zip" cmd="unzip -o #{file} -d #{path}" elsif format==".rar" cmd="unrar -y e #{file} #{path}" end system cmd end
upload(file, filepath="", rule="jpg|xls", minsize=1, maxsize=4000)
click to toggle source
# File lib/struggle/tfile.rb, line 3 def Tfile.upload(file, filepath="", rule="jpg|xls", minsize=1, maxsize=4000) result = Tfile.rule_validata(file, rule, minsize, maxsize) if result[:state] sname = Tfile.getname(file, filepath) begin unless Dir::exist?(filepath) unless system("mkdir -p #{filepath}") return {state: false, result: "目录创建失败,请于管理员联系"} end end File.open(filepath+sname, "wb") do |f| f.write(file.read) end return {state: true, result: sname} rescue return {state: false, result: "写入文件失败:#{$!}"} end else return {state: false, result: result[:message]} end end