diff --git a/app.lua b/app.lua index eec390e..04c0c10 100644 --- a/app.lua +++ b/app.lua @@ -1,23 +1,18 @@ local lapis = require("lapis") local respond_to = lapis.application.respond_to local app = lapis.Application() -local config = require("lapis.config").get() local db = require("lapis.db") -local Model = require("lapis.db.model").Model -local Users = require("models.users") -local Packages = require("models.packages") local login = require("controllers.login") local logout = require("controllers.logout") local register = require("controllers.register") local uploadpackage = require("controllers.uploadpackage") local changepackage = require("controllers.changepackage") -local deletepackage = require("controllers.deletepackage") local packages = require("controllers.packages") local account = require("controllers.account") local verify_email = require("controllers.verifyemail") local app_helpers = require("lapis.application") local resend_verification = require("controllers.resendverification") -local capture_errors, assert_error = app_helpers.capture_errors, app_helpers.assert_error +local capture_errors = app_helpers.capture_errors math.randomseed(os.clock()*1000000) app:enable("etlua") diff --git a/controllers/changepackage.lua b/controllers/changepackage.lua index 5272db9..93dfa21 100644 --- a/controllers/changepackage.lua +++ b/controllers/changepackage.lua @@ -1,72 +1,11 @@ local app_helpers = require("lapis.application") local validate = require("lapis.validate") local capture_errors, assert_error = app_helpers.capture_errors, app_helpers.assert_error -local lfs = require('lfs') local Packages = require("models.packages") local Users = require("models.users") +local FileUtils = require("helper.fileUtils") --- if you can change the name of a thing to itself, it is a file or folder --- best work around I could find for testing this -local function isFileOrDir(name) - if type(name)~="string" then return false end - return os.rename(name, name) -end - --- use LFS's chdir() function to determine if the target is a directory --- there is no check for if something is a file or directory in lua or LFS by default -local function isDir(name) - if type(name)~="string" then return false end - local cd = lfs.currentdir() - local is, err = lfs.chdir(name) - lfs.chdir(cd) - return is, err -end - --- If it isn't a directory, but we can rename it to itself, then it is a file -local function isFile(name) - if not isDir(name) then - return os.rename(name,name) and true or false - end - return false -end - --- use the above functions to check if the folders exist and --- create them if they do not. -local function save_file(self) - if not self.params.file then return end - -- record our current directory so we can change back to it - local cwd = lfs.currentdir() - local file_extension = string.match(self.params.file.filename, ".+%.(.+)") - - -- start in the data directory - assert_error(lfs.chdir(self.config.data_dir)) - - -- create username directory if it doesn't exist, validate it's a dir and enter it - if not isFileOrDir(self.session.name) then - lfs.mkdir(self.session.name) - end - assert_error(isDir(self.session.name), self.i18n("err_save_file")) - lfs.chdir(self.session.name) - - -- and again for the pkg name - if not isFileOrDir(self.params.name) then - lfs.mkdir(self.params.name) - end - assert_error(isDir(self.params.name), self.i18n("err_save_file")) - lfs.chdir(self.params.name) - - -- and again for the pkg version - if not isFileOrDir(self.params.version) then - lfs.mkdir(self.params.version) - end - assert_error(isDir(self.params.version), self.i18n("err_save_file")) - lfs.chdir(self.params.version) - local filename = string.format("%s-%s.%s", self.params.name, self.params.version, file_extension) - local file = io.open(filename, 'w') - file:write(self.params.file.content) - file:close() - lfs.chdir(cwd) -end +local save_file = FileUtils.save_file return { GET = capture_errors(function(self) diff --git a/controllers/uploadpackage.lua b/controllers/uploadpackage.lua index 80be12e..3f3dbd0 100644 --- a/controllers/uploadpackage.lua +++ b/controllers/uploadpackage.lua @@ -1,85 +1,11 @@ local app_helpers = require("lapis.application") local validate = require("lapis.validate") -local capture_errors, assert_error, yield_error = - app_helpers.capture_errors, app_helpers.assert_error, app_helpers.yield_error -local lfs = require('lfs') +local capture_errors, assert_error = app_helpers.capture_errors, app_helpers.assert_error local Packages = require("models.packages") local Users = require("models.users") +local FileUtils = require("helper.fileUtils") --- if you can change the name of a thing to itself, it is a file or folder --- best work around I could find for testing this -local function isFileOrDir(name) - if type(name)~="string" then return false end - return os.rename(name, name) -end - --- use LFS's chdir() function to determine if the target is a directory --- there is no check for if something is a file or directory in lua or LFS by default -local function isDir(name) - if type(name)~="string" then return false end - local cd = lfs.currentdir() - local is, err = lfs.chdir(name) - lfs.chdir(cd) - return is, err -end - --- If it isn't a directory, but we can rename it to itself, then it is a file -local function isFile(name) - if not isDir(name) then - return os.rename(name,name) and true or false - end - return false -end - --- use the above functions to check if the folders exist and --- create them if they do not. -local function save_file(self) - -- record our current directory so we can change back to it - local cwd = lfs.currentdir() - local file_extension = string.match(self.params.file.filename, ".+%.(.+)") - - -- start in the data directory - local success, message = lfs.chdir(self.config.data_dir) - if not success then lfs.chdir(cwd); yield_error("Couldn't enter data directory: "..message) end - - local handle = io.popen("ps") - local result = handle:read("*a") - handle:close() - print("ps result: "..result) - - -- create username directory if it doesn't exist, validate it's a dir and enter it - if not isFileOrDir(self.session.name) then - local success, message = lfs.mkdir(self.session.name) - if not success then lfs.chdir(cwd); yield_error("Couldn't create username directory: "..message) end - end - local success, message = lfs.chdir(self.session.name) - if not success then lfs.chdir(cwd); yield_error("Couldn't enter username directory: "..message) end - - -- and again for the pkg name - if not isFileOrDir(self.params.name) then - local success, message = lfs.mkdir(self.params.name) - if not success then lfs.chdir(cwd); yield_error("Couldn't create package directory: "..message) end - end - local success, message = lfs.chdir(self.params.name) - if not success then lfs.chdir(cwd); yield_error("Couldn't enter package directory: "..message) end - - -- and again for the pkg version - if not isFileOrDir(self.params.version) then - local success, message = lfs.mkdir(self.params.version) - if not success then lfs.chdir(cwd); yield_error("Couldn't create package version directory: "..message) end - end - local success, message = lfs.chdir(self.params.version) - if not success then lfs.chdir(cwd); yield_error("Couldn't enter package version directory: "..message) end - - local filename = string.format("%s-%s.%s", self.params.name, self.params.version, file_extension) - local file, message = io.open(filename, 'w') - if not file then lfs.chdir(cwd); yield_error("Couldn't open package file for writing: "..message) end - file:write(self.params.file.content) - file:flush() - file:close() - local success, message = lfs.chdir(cwd) - if not success then yield_error("Couldn't change back to the application's working directory: ".. message) end -end +local save_file = FileUtils.save_file return { GET = capture_errors(function(self) diff --git a/helper/fileUtils.lua b/helper/fileUtils.lua new file mode 100644 index 0000000..9073087 --- /dev/null +++ b/helper/fileUtils.lua @@ -0,0 +1,76 @@ +local lfs = require('lfs') +local app_helpers = require("lapis.application") +local yield_error = + app_helpers.yield_error +local FileUtils = {} + + +-- if you can change the name of a thing to itself, it is a file or folder +-- best work around I could find for testing this +function FileUtils:isFileOrDir(name) + if type(name)~="string" then return false end + return os.rename(name, name) +end + +-- use LFS's chdir() function to determine if the target is a directory +-- there is no check for if something is a file or directory in lua or LFS by default +function FileUtils:isDir(name) + if type(name)~="string" then return false end + local cd = lfs.currentdir() + local is, err = lfs.chdir(name) + lfs.chdir(cd) + return is, err +end + +-- use the above functions to check if the folders exist and +-- create them if they do not. +function FileUtils:save_file(app) + if not app then return end + -- record our current directory so we can change back to it + local cwd = lfs.currentdir() + local file_extension = string.match(app.params.file.filename, ".+%.(.+)") + + -- start in the data directory + local success, message = lfs.chdir(app.config.data_dir) + if not success then lfs.chdir(cwd); yield_error("Couldn't enter data directory: "..message) end + + local handle = io.popen("ps") + local result = handle:read("*a") + handle:close() + print("ps result: "..result) + + -- create username directory if it doesn't exist, validate it's a dir and enter it + if not self:isFileOrDir(app.session.name) then + local success, message = lfs.mkdir(app.session.name) + if not success then lfs.chdir(cwd); yield_error("Couldn't create username directory: "..message) end + end + local success, message = lfs.chdir(app.session.name) + if not success then lfs.chdir(cwd); yield_error("Couldn't enter username directory: "..message) end + + -- and again for the pkg name + if not self:isFileOrDir(app.params.name) then + local success, message = lfs.mkdir(app.params.name) + if not success then lfs.chdir(cwd); yield_error("Couldn't create package directory: "..message) end + end + local success, message = lfs.chdir(app.params.name) + if not success then lfs.chdir(cwd); yield_error("Couldn't enter package directory: "..message) end + + -- and again for the pkg version + if not self:isFileOrDir(app.params.version) then + local success, message = lfs.mkdir(app.params.version) + if not success then lfs.chdir(cwd); yield_error("Couldn't create package version directory: "..message) end + end + local success, message = lfs.chdir(app.params.version) + if not success then lfs.chdir(cwd); yield_error("Couldn't enter package version directory: "..message) end + + local filename = string.format("%s-%s.%s", app.params.name, app.params.version, file_extension) + local file, message = io.open(filename, 'w') + if not file then lfs.chdir(cwd); yield_error("Couldn't open package file for writing: "..message) end + file:write(app.params.file.content) + file:flush() + file:close() + local success, message = lfs.chdir(cwd) + if not success then yield_error("Couldn't change back to the application's working directory: ".. message) end +end + +return FileUtils