multiple syntax fixes

I never said I was *good* at ruby...
This commit is contained in:
snow flurry 2021-03-24 21:47:17 -07:00
parent 6b75dfd6da
commit f25803aaa5
2 changed files with 62 additions and 63 deletions

View file

@ -12,22 +12,22 @@ $cgi = CGI.new
# convenience function to print an error to CGI log and quit
def die(status, error)
puts $cgi.http_header {
:type => "text/plain",
:status => status
}
$cgi.print $cgi.http_header(
"type" => "text/plain",
"status" => status
)
if status == "SERVER_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
puts error
$cgi.print error
end
exit
end
def get_yaml(path)
if not File.file? path
die "SERVER_ERROR", "Application not configured"
die("SERVER_ERROR", "Application not configured")
end
begin
@ -35,11 +35,11 @@ def get_yaml(path)
File.open(path).read
)
return cfg
rescue =>
die "SERVER_ERROR", "Config file broken ;_;"
rescue
die("SERVER_ERROR", "Config file broken ;_;")
end
# should be unreachable
die "SERVER_ERROR", "get_yaml broken ;_;"
die("SERVER_ERROR", "get_yaml broken ;_;")
end
# sanitizes string so all that's left is alphanumerics and .
@ -51,52 +51,52 @@ end
def main()
# consistency checks for request
# if not authenticated, ignore
if $cgi.REMOTE_USER.nil? || $cgi.REMOTE_USER.empty?
die "FORBIDDEN", ""
if $cgi.remote_user.nil? || $cgi.remote_user.empty?
die("FORBIDDEN", "")
end
# even though REMOTE_USER comes from the upstream, don't trust it
clean_name = sanitize $cgi.REMOTE_USER
# even though remote_user comes from the upstream, don't trust it
clean_name = sanitize $cgi.remote_user
# Only POST supported for this API
if $cgi.REQUEST_METHOD != "POST"
die "METHOD_NOT_ALLOWED", "Method not allowed"
if $cgi.request_method != "POST"
die("METHOD_NOT_ALLOWED", "Method not allowed")
end
if $cgi.CONTENT_TYPE != "multipart/form-data"
die "NOT_ACCEPTABLE", "Please use multipart/form-data"
if !$cgi.content_type.start_with?("multipart/form-data")
die("NOT_ACCEPTABLE", "Please use multipart/form-data, you're using " + $cgi.content_type)
end
cfg = get_yaml CONF_PATH
# 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!"
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!"
end
# 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
die "SERVER_ERROR", "User directory " + user_dir + " doesn't exist!"
die("SERVER_ERROR", "User directory " + user_dir + " doesn't exist!")
end
# get file data
if $cgi.params['sendfile'].nil? || $cgi.params['sendfile'].empty?
die "NOT_ACCEPTABLE", "Missing sendfile?"
die("NOT_ACCEPTABLE", "Missing sendfile?")
end
upfile = $cgi.params['sendfile'][0]
# 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)
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
lm.close
@ -105,7 +105,7 @@ def main()
# don't overwrite an existing file unless they asked for it
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
# finally, store the file
@ -118,7 +118,7 @@ def main()
$cgi.print $cgi.http_header("status" => "OK",
"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
main

View file

@ -32,10 +32,11 @@ module LibMagic
class Magic
# 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
dbptr = FFI::Pointer.new 0x0
@magic_inst = ffi_magic_open(flags)
# dbptr = FFI::Pointer.new 0x0
dbptr = nil
@magic_inst = LibMagic::ffi_magic_open(flags)
if @magic_inst.null?
raise "magic_open() failed"
end
@ -46,39 +47,36 @@ module LibMagic
end
# load magic database
if db_path is not nil
if !db_path.nil?
dbptr = db_path
end
res = ffi_magic_load(@magic_inst, dbptr)
res = LibMagic::ffi_magic_load(@magic_inst, dbptr)
if res != 0
self.throw_error
end
end
# Gets MIME type for a given stream
def self.get_mime_type(stream)
def get_mime_type(stream)
res = nil
if stream.fileno.nil? # magic_buffer
buf = stream.string
buflen = buf.bytesize
res = ffi_magic_buffer(@magic_inst, buf, buflen)
res, ptr = LibMagic::ffi_magic_buffer(@magic_inst, buf, buflen)
else # magic_descriptor
res = ffi_magic_descriptor(@magic_inst, stream.fileno)
res, ptr = LibMagic::ffi_magic_descriptor(@magic_inst, stream.fileno)
end
if res.nil? || res.null?
if res.nil? || res.empty?
self.throw_error
else
retstr, retfree = FFI::StrPtrConverter.from_native(res, nil)
return retstr
end
# unreachable
return nil
return res
end
def self.close()
def close()
unless @magic_inst.nil?
ffi_magic_close(@magic_inst)
LibMagic::ffi_magic_close(@magic_inst)
@magic_inst = nil
end
end
@ -86,33 +84,34 @@ module LibMagic
private
# convenience method for throwing MagicErrors
def self.throw_error()
def throw_error()
if @magic_inst.null?
raise LibMagic::MagicError.new
else
errptr = ffi_magic_error(@magic_inst)
errno = ffi_magic_errno(@magic_inst)
errptr = LibMagic::ffi_magic_error(@magic_inst)
errno = LibMagic::ffi_magic_errno(@magic_inst)
errstr, errfree = FFI::StrPtrConverter.from_native(errptr, nil)
raise LibMagic::MagicError.new(errstr, errno)
end
end
## ffi
# i/o open/close
attach_function :ffi_magic_open, :magic_open, [:int], :pointer
attach_function :ffi_magic_close, :magic_close, [:pointer], :void
# error info (a la GetLastError/win32)
attach_function :ffi_magic_error, :magic_error, [:pointer], :strptr
attach_function :ffi_magic_errno, :magic_errno, [:pointer], :int
# libmagic flags
attach_function :ffi_magic_getflags, :magic_getflags, [:pointer], :int
attach_function :ffi_magic_setflags, :magic_setflags, [:pointer, :int], :int
# load the libmagic db -- MUST BE DONE BEFORE GETTING MAGIC INFO
attach_function :ffi_magic_load, :magic_load, [:pointer, :string], :int
# get magic info
attach_function :ffi_magic_file, :magic_file, [:pointer, :string], :strptr
attach_function :ffi_magic_buffer, :magic_buffer, [:pointer, :pointer, :size_t], :strptr
attach_function :ffi_magic_descriptor, :magic_descriptor, [:pointer, :int], :strptr
end
## ffi
# i/o open/close
attach_function :ffi_magic_open, :magic_open, [:int], :pointer
attach_function :ffi_magic_close, :magic_close, [:pointer], :void
# error info (a la GetLastError/win32)
attach_function :ffi_magic_error, :magic_error, [:pointer], :strptr
attach_function :ffi_magic_errno, :magic_errno, [:pointer], :int
# libmagic flags
attach_function :ffi_magic_getflags, :magic_getflags, [:pointer], :int
attach_function :ffi_magic_setflags, :magic_setflags, [:pointer, :int], :int
# load the libmagic db -- MUST BE DONE BEFORE GETTING MAGIC INFO
attach_function :ffi_magic_load, :magic_load, [:pointer, :string], :int
# get magic info
attach_function :ffi_magic_file, :magic_file, [:pointer, :string], :strptr
attach_function :ffi_magic_buffer, :magic_buffer, [:pointer, :pointer, :size_t], :strptr
attach_function :ffi_magic_descriptor, :magic_descriptor, [:pointer, :int], :strptr
end