simple_fileupload 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/lib/simple_fileupload.rb +99 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZWU0NmIwMDlkZGY4MTg4NWZjYzc4ODBhZjZiMTdjYjNiMjk3NTBmYw==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZWFhYzY4ZWM0YzJhY2JlNmZkYzBmZjc4NzIzZDdkYzQ4NzI4YzNmOQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YTk4YzY3OTQ2NTgzOWVjOWRjMTg2OTZlZDQwNzRkOWQzZWEzZTJhMjBhMjI3
|
10
|
+
MWViNDE5Yzk4ZGQ4MTQ4ZjM4MGViMTIxYmE2ZjA4OTM3MTQzMmM3MzJmMWEx
|
11
|
+
YWUyY2U0OTc5MzFiZjczZjM3YTAxYjk1ODlmN2RjM2IzMjNhYzU=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
N2M3N2RkZTI5NjQ2NGU4MTA0ODk5MmI1OTcyNzRjOWE0ZTY5OTg5NzdkMzhm
|
14
|
+
MjVkMTk5OTMyNDAwNDUxNmU3NjkxMzg0NzcwOTcxOWJiZTM4NmUxZWU4ZTZl
|
15
|
+
YTBjNjAyNjM4NzJjZTdkYTQwMTliMTY2OWMzOGU3ZDE4MDE2MmI=
|
@@ -0,0 +1,99 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
class UploadException < RuntimeError; end
|
3
|
+
#上传文件
|
4
|
+
class SimpleFileupload
|
5
|
+
require 'fileutils'
|
6
|
+
|
7
|
+
def initialize(configs)
|
8
|
+
@upload_path = "#{Rails.root}/data"
|
9
|
+
@type = @max_size = @random_dir = nil
|
10
|
+
configs.each do |key, value|
|
11
|
+
if config_keys.include? key
|
12
|
+
self.send("#{key}=", value)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
FileUtils.mkdir(@upload_path, mode: 0766) unless File.directory? @upload_path
|
16
|
+
FileUtils.chmod(0766, @upload_path) unless File.writable_real?(@upload_path)
|
17
|
+
((0..9).to_a + ('a'..'z').to_a).each &method(:check_and_mkdir) if random_dir?
|
18
|
+
end
|
19
|
+
|
20
|
+
def upload(file)
|
21
|
+
mime_type = get_mime_type file
|
22
|
+
raise UploadException, '文件不是图片格式' if @type == 'image' && !mime_type_is_image?(mime_type)
|
23
|
+
file_size = File.size file.tempfile
|
24
|
+
file_name = file.original_filename
|
25
|
+
raise UploadException, "文件大小超出限制的大小#{@max_size}" if (@max_size && file_size > @max_size.to_i)
|
26
|
+
raise UploadException, "上传目录不是目录或不能写入" unless (File.directory?(@upload_path) && File.writable_real?(@upload_path))
|
27
|
+
real_file_name = generage_file_name file.original_filename
|
28
|
+
random_path = random_dir
|
29
|
+
@upload_path = File.join(@upload_path, random_path) if random_path
|
30
|
+
file_path = File.join(@upload_path, real_file_name)
|
31
|
+
File.open(file_path, 'wb') do |f|
|
32
|
+
f.write file.read
|
33
|
+
end
|
34
|
+
rescue Exception => e
|
35
|
+
raise UploadException, '文件上传失败:' + e.message
|
36
|
+
else
|
37
|
+
{file_name: file_name, real_file_name: real_file_name, file_size: file_size, upload_path: @upload_path, file_full_path: file_path, mime_type: mime_type}
|
38
|
+
end
|
39
|
+
|
40
|
+
protected
|
41
|
+
|
42
|
+
def generage_file_name(original_filename)
|
43
|
+
if original_filename
|
44
|
+
filename = SecureRandom.uuid + File.extname(original_filename)
|
45
|
+
if File.exist? File.join(@upload_path, filename)
|
46
|
+
generage_file_name(original_filename)
|
47
|
+
else
|
48
|
+
filename
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def is_image?(file)
|
54
|
+
get_mime_type(file) =~ /^image/
|
55
|
+
end
|
56
|
+
|
57
|
+
def get_mime_type(file)
|
58
|
+
`file --mime -b #{file.tempfile.path}`
|
59
|
+
end
|
60
|
+
|
61
|
+
def mime_type_is_image?(mime_type)
|
62
|
+
mime_type =~ /^image/
|
63
|
+
end
|
64
|
+
|
65
|
+
def config_keys
|
66
|
+
[:upload_path, :max_size, :type, :random_dir]
|
67
|
+
end
|
68
|
+
|
69
|
+
def upload_path=(path)
|
70
|
+
@upload_path = path
|
71
|
+
end
|
72
|
+
|
73
|
+
def max_size=(size)
|
74
|
+
@max_size = size
|
75
|
+
end
|
76
|
+
|
77
|
+
def type=(type)
|
78
|
+
@type = type
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
def random_dir=(random_dir)
|
83
|
+
@random_dir = random_dir
|
84
|
+
end
|
85
|
+
|
86
|
+
def random_dir?
|
87
|
+
@random_dir
|
88
|
+
end
|
89
|
+
|
90
|
+
def check_and_mkdir(dir)
|
91
|
+
dir_name = File.join(@upload_path, dir.to_s)
|
92
|
+
FileUtils.mkdir(dir_name, mode: 0766) unless File.directory? dir_name
|
93
|
+
FileUtils.chmod(0766, dir_name)
|
94
|
+
end
|
95
|
+
|
96
|
+
def random_dir
|
97
|
+
random_dir? ? ((0..9).to_a + ('a'..'z').to_a).sample : nil
|
98
|
+
end
|
99
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_fileupload
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- fxhover
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-19 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple rails fileupload gem
|
14
|
+
email: 736698959@qq.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/simple_fileupload.rb
|
20
|
+
homepage: http://rubygems.org/gems/simpile_fileupload
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 1.3.6
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.2.2
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: strdes!
|
44
|
+
test_files: []
|