diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/guff/apis.cr | 21 | ||||
-rw-r--r-- | src/guff/models/file.cr | 37 | ||||
-rw-r--r-- | src/guff/models/site.cr | 15 | ||||
-rw-r--r-- | src/guff/views/pages/admin.cr | 6 |
4 files changed, 56 insertions, 23 deletions
diff --git a/src/guff/apis.cr b/src/guff/apis.cr index bbedd3f..f5424dd 100644 --- a/src/guff/apis.cr +++ b/src/guff/apis.cr @@ -190,26 +190,32 @@ module Guff::APIs module FileAPI def do_file_list(params : HTTP::Params) - @context.models.file.list(URI.unescape(params["path"])) + @context.models.file.list( + site_id: params["site_id"].not_nil!.to_i64, + path: URI.unescape(params["path"]), + ) end def do_file_add(params : HTTP::Params) # TODO: upload data will be cached in context, then retreived in # file model @context.models.file.add( - URI.unescape(params["path"].not_nil!), - params["upload_data"].not_nil!, + site_id: params["site_id"].not_nil!.to_i64, + path: URI.unescape(params["path"].not_nil!), + upload_data: params["upload_data"].not_nil!, ) end def do_file_mkdir(params : HTTP::Params) - # TODO: uploaded data is cached in context, then retreived in file - # model - @context.models.file.mkdir(URI.unescape(params["path"].not_nil!)) + @context.models.file.mkdir( + site_id: params["site_id"].not_nil!.to_i64, + path: URI.unescape(params["path"].not_nil!), + ) end def do_file_move(params : HTTP::Params) @context.models.file.move( + site_id: params["site_id"].not_nil!.to_i64, src_path: URI.unescape(params["src_path"].not_nil!), dst_path: URI.unescape(params["dst_path"].not_nil!), ) @@ -217,7 +223,8 @@ module Guff::APIs def do_file_delete(params : HTTP::Params) @context.models.file.delete( - URI.unescape(params["path"].not_nil!), + site_id: params["site_id"].not_nil!.to_i64, + path: URI.unescape(params["path"].not_nil!), ) end end diff --git a/src/guff/models/file.cr b/src/guff/models/file.cr index b001c2f..f55037c 100644 --- a/src/guff/models/file.cr +++ b/src/guff/models/file.cr @@ -1,9 +1,9 @@ require "./model" class Guff::Models::FileModel < Guff::Models::Model - def list(path : String) + def list(site_id : Int64, path : String) # build absolute path - abs_path = expand_path(path) + abs_path = expand_path(site_id, path) # make sure directory exists unless Dir.exists?(abs_path) @@ -11,8 +11,12 @@ class Guff::Models::FileModel < Guff::Models::Model end # build base file path - base_path = File.expand_path(path) - base_url = File.join("/guff/api/file/download", base_path) + base_path = File.expand_path(path, "/") + base_url = File.join( + "/guff/api/file/download", + @context.models.site.get_name(site_id), + base_path + ) Dir.entries(abs_path).select { |file| # exclude hidden files @@ -34,14 +38,18 @@ class Guff::Models::FileModel < Guff::Models::Model } end - def add(path : String, upload_data : String) + def add( + site_id : Int64, + path : String, + upload_data : String + ) # TODO nil end - def mkdir(path : String) + def mkdir(site_id : Int64, path : String) # build absolute path - abs_path = expand_path(path) + abs_path = expand_path(site_id, path) # create directory Dir.mkdir(abs_path) @@ -49,10 +57,10 @@ class Guff::Models::FileModel < Guff::Models::Model nil end - def move(src_path : String, dst_path : String) + def move(site_id : Int64, src_path : String, dst_path : String) # build absolute paths - abs_src_path = expand_path(src_path) - abs_dst_path = expand_path(dst_path) + abs_src_path = expand_path(site_id, src_path) + abs_dst_path = expand_path(site_id, dst_path) # make sure src path exists unless File.exists?(abs_src_path) @@ -71,9 +79,9 @@ class Guff::Models::FileModel < Guff::Models::Model nil end - def delete(path : String) + def delete(site_id : Int64, path : String) # build absolute path - abs_path = expand_path(path) + abs_path = expand_path(site_id, path) if Dir.exists?(abs_path) Dir.rmdir(abs_path) @@ -87,14 +95,15 @@ class Guff::Models::FileModel < Guff::Models::Model nil end - private def expand_path(path : String) + private def expand_path(site_id : Int64, path : String) raise "invalid path" if path.includes?('\0') # return expanded path File.join( @context.config.data_dir, "files", - File.expand_path(path) + @context.models.site.get_name(site_id), + File.expand_path(path, "/") ) end end diff --git a/src/guff/models/site.cr b/src/guff/models/site.cr index 431911d..4672fa4 100644 --- a/src/guff/models/site.cr +++ b/src/guff/models/site.cr @@ -61,7 +61,16 @@ class Guff::Models::SiteModel < Guff::Models::Model JOIN themes b ON (b.theme_id = a.theme_id) WHERE a.site_id = ? - " + ", + + get_name: " + SELECT name + + FROM sites a + + WHERE site_id = ? + AND is_active + ", } def get_id(host : String?) : Int64? @@ -89,4 +98,8 @@ class Guff::Models::SiteModel < Guff::Models::Model def get(site_id : Int64) @context.dbs.ro.row(SQL[:get], [site_id.to_s]).not_nil! end + + def get_name(site_id : Int64) : String + @context.dbs.ro.one(SQL[:get_name], [site_id.to_s]).as(String) + end end diff --git a/src/guff/views/pages/admin.cr b/src/guff/views/pages/admin.cr index e385cea..f2296a8 100644 --- a/src/guff/views/pages/admin.cr +++ b/src/guff/views/pages/admin.cr @@ -99,7 +99,11 @@ class Guff::Views::Pages::Admin < Guff::Views::HTMLView end private def page_data - { post_types: POST_TYPES}.to_json + { + post_types: POST_TYPES, + sites: @context.models.site.get_sites, + default_site_id: @context.models.site.get_default_id, + }.to_json end ECR.def_to_s("src/views/pages/admin.ecr") |