multiple syntax fixes
I never said I was *good* at ruby...
This commit is contained in:
parent
6b75dfd6da
commit
f25803aaa5
|
@ -12,22 +12,22 @@ $cgi = CGI.new
|
||||||
|
|
||||||
# convenience function to print an error to CGI log and quit
|
# convenience function to print an error to CGI log and quit
|
||||||
def die(status, error)
|
def die(status, error)
|
||||||
puts $cgi.http_header {
|
$cgi.print $cgi.http_header(
|
||||||
:type => "text/plain",
|
"type" => "text/plain",
|
||||||
:status => status
|
"status" => status
|
||||||
}
|
)
|
||||||
if status == "SERVER_ERROR"
|
if status == "SERVER_ERROR"
|
||||||
$stderr.puts "[aperture:fatal] " + error
|
$stderr.puts "[aperture:fatal] " + error
|
||||||
puts "Something went wrong! Ask the server admin to check the logs for more info."
|
$cgi.print "Something went wrong! Ask the server admin to check the logs for more info."
|
||||||
else
|
else
|
||||||
puts error
|
$cgi.print error
|
||||||
end
|
end
|
||||||
exit
|
exit
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_yaml(path)
|
def get_yaml(path)
|
||||||
if not File.file? path
|
if not File.file? path
|
||||||
die "SERVER_ERROR", "Application not configured"
|
die("SERVER_ERROR", "Application not configured")
|
||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
@ -35,11 +35,11 @@ def get_yaml(path)
|
||||||
File.open(path).read
|
File.open(path).read
|
||||||
)
|
)
|
||||||
return cfg
|
return cfg
|
||||||
rescue =>
|
rescue
|
||||||
die "SERVER_ERROR", "Config file broken ;_;"
|
die("SERVER_ERROR", "Config file broken ;_;")
|
||||||
end
|
end
|
||||||
# should be unreachable
|
# should be unreachable
|
||||||
die "SERVER_ERROR", "get_yaml broken ;_;"
|
die("SERVER_ERROR", "get_yaml broken ;_;")
|
||||||
end
|
end
|
||||||
|
|
||||||
# sanitizes string so all that's left is alphanumerics and .
|
# sanitizes string so all that's left is alphanumerics and .
|
||||||
|
@ -51,52 +51,52 @@ end
|
||||||
def main()
|
def main()
|
||||||
# consistency checks for request
|
# consistency checks for request
|
||||||
# if not authenticated, ignore
|
# if not authenticated, ignore
|
||||||
if $cgi.REMOTE_USER.nil? || $cgi.REMOTE_USER.empty?
|
if $cgi.remote_user.nil? || $cgi.remote_user.empty?
|
||||||
die "FORBIDDEN", ""
|
die("FORBIDDEN", "")
|
||||||
end
|
end
|
||||||
|
|
||||||
# even though REMOTE_USER comes from the upstream, don't trust it
|
# even though remote_user comes from the upstream, don't trust it
|
||||||
clean_name = sanitize $cgi.REMOTE_USER
|
clean_name = sanitize $cgi.remote_user
|
||||||
|
|
||||||
# Only POST supported for this API
|
# Only POST supported for this API
|
||||||
if $cgi.REQUEST_METHOD != "POST"
|
if $cgi.request_method != "POST"
|
||||||
die "METHOD_NOT_ALLOWED", "Method not allowed"
|
die("METHOD_NOT_ALLOWED", "Method not allowed")
|
||||||
end
|
end
|
||||||
|
|
||||||
if $cgi.CONTENT_TYPE != "multipart/form-data"
|
if !$cgi.content_type.start_with?("multipart/form-data")
|
||||||
die "NOT_ACCEPTABLE", "Please use multipart/form-data"
|
die("NOT_ACCEPTABLE", "Please use multipart/form-data, you're using " + $cgi.content_type)
|
||||||
end
|
end
|
||||||
|
|
||||||
cfg = get_yaml CONF_PATH
|
cfg = get_yaml CONF_PATH
|
||||||
|
|
||||||
# make sure expected config entries are there
|
# make sure expected config entries are there
|
||||||
if cfg[:save_path].nil? or cfg[:save_path].empty?
|
if cfg['save_path'].nil? or cfg['save_path'].empty?
|
||||||
die "SERVER_ERROR", "config: save_path not set!"
|
die "SERVER_ERROR", "config: save_path not set!"
|
||||||
end
|
end
|
||||||
|
|
||||||
if cfg[:root_url].nil? or cfg[:root_url].empty?
|
if cfg['root_url'].nil? or cfg['root_url'].empty?
|
||||||
die "SERVER_ERROR", "config: root_url not set!"
|
die "SERVER_ERROR", "config: root_url not set!"
|
||||||
end
|
end
|
||||||
|
|
||||||
# XXX: configurable prefix?
|
# XXX: configurable prefix?
|
||||||
user_dir = cfg[:save_path] + "/" + USER_PREFIX + clean_name
|
user_dir = cfg['save_path'] + "/" + USER_PREFIX + clean_name
|
||||||
|
|
||||||
if not File.directory? user_dir
|
if not File.directory? user_dir
|
||||||
die "SERVER_ERROR", "User directory " + user_dir + " doesn't exist!"
|
die("SERVER_ERROR", "User directory " + user_dir + " doesn't exist!")
|
||||||
end
|
end
|
||||||
|
|
||||||
# get file data
|
# get file data
|
||||||
if $cgi.params['sendfile'].nil? || $cgi.params['sendfile'].empty?
|
if $cgi.params['sendfile'].nil? || $cgi.params['sendfile'].empty?
|
||||||
die "NOT_ACCEPTABLE", "Missing sendfile?"
|
die("NOT_ACCEPTABLE", "Missing sendfile?")
|
||||||
end
|
end
|
||||||
|
|
||||||
upfile = $cgi.params['sendfile'][0]
|
upfile = $cgi.params['sendfile'][0]
|
||||||
|
|
||||||
# get the mime type from the file contents, not HTTP
|
# get the mime type from the file contents, not HTTP
|
||||||
lm = LibMagic::Magic.new(cfg[:magic_path])
|
lm = LibMagic::Magic.new(cfg['magic_path'])
|
||||||
ftype = lm.get_mime_type(upfile)
|
ftype = lm.get_mime_type(upfile)
|
||||||
if ftype.match(/^(image|text|audio|video)\/[a-zA-Z0-9\-\.]+$/).nil?
|
if ftype.match(/^(image|text|audio|video)\/[a-zA-Z0-9\-\.]+$/).nil?
|
||||||
die "NOT_ACCEPTABLE", "MIME type " + ftype + " not supported here"
|
die("NOT_ACCEPTABLE", "MIME type " + ftype + " not supported here")
|
||||||
end
|
end
|
||||||
lm.close
|
lm.close
|
||||||
|
|
||||||
|
@ -105,7 +105,7 @@ def main()
|
||||||
|
|
||||||
# don't overwrite an existing file unless they asked for it
|
# don't overwrite an existing file unless they asked for it
|
||||||
if $cgi.params['overwrite'] != "true" and File.exists? full_path
|
if $cgi.params['overwrite'] != "true" and File.exists? full_path
|
||||||
die "NOT_ACCEPTABLE" "File " + clean_file + " already exists!"
|
die("NOT_ACCEPTABLE" "File " + clean_file + " already exists!")
|
||||||
end
|
end
|
||||||
|
|
||||||
# finally, store the file
|
# finally, store the file
|
||||||
|
@ -118,7 +118,7 @@ def main()
|
||||||
$cgi.print $cgi.http_header("status" => "OK",
|
$cgi.print $cgi.http_header("status" => "OK",
|
||||||
"type" => "text/plain")
|
"type" => "text/plain")
|
||||||
|
|
||||||
$cgi.print cfg[:root_url] + USER_PREFIX + clean_name + "/" + clean_file
|
$cgi.print cfg['root_url'] + USER_PREFIX + clean_name + "/" + clean_file
|
||||||
end
|
end
|
||||||
|
|
||||||
main
|
main
|
||||||
|
|
|
@ -32,10 +32,11 @@ module LibMagic
|
||||||
|
|
||||||
class Magic
|
class Magic
|
||||||
# initialize libmagic
|
# initialize libmagic
|
||||||
def initialize(db_path = nil, flags = LibMagic::MAGIC_MIME)
|
def initialize(db_path = nil, flags = LibMagic::MAGIC_MIME_TYPE)
|
||||||
# nullptr for default db path
|
# nullptr for default db path
|
||||||
dbptr = FFI::Pointer.new 0x0
|
# dbptr = FFI::Pointer.new 0x0
|
||||||
@magic_inst = ffi_magic_open(flags)
|
dbptr = nil
|
||||||
|
@magic_inst = LibMagic::ffi_magic_open(flags)
|
||||||
if @magic_inst.null?
|
if @magic_inst.null?
|
||||||
raise "magic_open() failed"
|
raise "magic_open() failed"
|
||||||
end
|
end
|
||||||
|
@ -46,39 +47,36 @@ module LibMagic
|
||||||
end
|
end
|
||||||
|
|
||||||
# load magic database
|
# load magic database
|
||||||
if db_path is not nil
|
if !db_path.nil?
|
||||||
dbptr = db_path
|
dbptr = db_path
|
||||||
end
|
end
|
||||||
|
|
||||||
res = ffi_magic_load(@magic_inst, dbptr)
|
res = LibMagic::ffi_magic_load(@magic_inst, dbptr)
|
||||||
if res != 0
|
if res != 0
|
||||||
self.throw_error
|
self.throw_error
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Gets MIME type for a given stream
|
# Gets MIME type for a given stream
|
||||||
def self.get_mime_type(stream)
|
def get_mime_type(stream)
|
||||||
res = nil
|
res = nil
|
||||||
if stream.fileno.nil? # magic_buffer
|
if stream.fileno.nil? # magic_buffer
|
||||||
buf = stream.string
|
buf = stream.string
|
||||||
buflen = buf.bytesize
|
buflen = buf.bytesize
|
||||||
res = ffi_magic_buffer(@magic_inst, buf, buflen)
|
res, ptr = LibMagic::ffi_magic_buffer(@magic_inst, buf, buflen)
|
||||||
else # magic_descriptor
|
else # magic_descriptor
|
||||||
res = ffi_magic_descriptor(@magic_inst, stream.fileno)
|
res, ptr = LibMagic::ffi_magic_descriptor(@magic_inst, stream.fileno)
|
||||||
end
|
end
|
||||||
if res.nil? || res.null?
|
if res.nil? || res.empty?
|
||||||
self.throw_error
|
self.throw_error
|
||||||
else
|
|
||||||
retstr, retfree = FFI::StrPtrConverter.from_native(res, nil)
|
|
||||||
return retstr
|
|
||||||
end
|
|
||||||
# unreachable
|
|
||||||
return nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.close()
|
return res
|
||||||
|
end
|
||||||
|
|
||||||
|
def close()
|
||||||
unless @magic_inst.nil?
|
unless @magic_inst.nil?
|
||||||
ffi_magic_close(@magic_inst)
|
LibMagic::ffi_magic_close(@magic_inst)
|
||||||
@magic_inst = nil
|
@magic_inst = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -86,17 +84,19 @@ module LibMagic
|
||||||
private
|
private
|
||||||
|
|
||||||
# convenience method for throwing MagicErrors
|
# convenience method for throwing MagicErrors
|
||||||
def self.throw_error()
|
def throw_error()
|
||||||
if @magic_inst.null?
|
if @magic_inst.null?
|
||||||
raise LibMagic::MagicError.new
|
raise LibMagic::MagicError.new
|
||||||
else
|
else
|
||||||
errptr = ffi_magic_error(@magic_inst)
|
errptr = LibMagic::ffi_magic_error(@magic_inst)
|
||||||
errno = ffi_magic_errno(@magic_inst)
|
errno = LibMagic::ffi_magic_errno(@magic_inst)
|
||||||
errstr, errfree = FFI::StrPtrConverter.from_native(errptr, nil)
|
errstr, errfree = FFI::StrPtrConverter.from_native(errptr, nil)
|
||||||
raise LibMagic::MagicError.new(errstr, errno)
|
raise LibMagic::MagicError.new(errstr, errno)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
## ffi
|
## ffi
|
||||||
|
|
||||||
# i/o open/close
|
# i/o open/close
|
||||||
|
@ -115,4 +115,3 @@ module LibMagic
|
||||||
attach_function :ffi_magic_buffer, :magic_buffer, [:pointer, :pointer, :size_t], :strptr
|
attach_function :ffi_magic_buffer, :magic_buffer, [:pointer, :pointer, :size_t], :strptr
|
||||||
attach_function :ffi_magic_descriptor, :magic_descriptor, [:pointer, :int], :strptr
|
attach_function :ffi_magic_descriptor, :magic_descriptor, [:pointer, :int], :strptr
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
Loading…
Reference in a new issue