diff options
author | Milo Casagrande <milo.casagrande@linaro.org> | 2015-01-14 16:17:28 +0100 |
---|---|---|
committer | Milo Casagrande <milo.casagrande@linaro.org> | 2015-01-14 16:17:28 +0100 |
commit | 58115d3209685dcd43ba8abb6cc14a68b2e92555 (patch) | |
tree | c3a3f390bd6818fca2bc671bf3b27d69fd61ff0c /app | |
parent | 7c6996578c05bbf4986a999efd4070b3c8cbb8f0 (diff) |
Refactor function name, add docs.
Change-Id: Id3dde02650a367b767631db5e41353685a3a3fc9
Diffstat (limited to 'app')
-rw-r--r-- | app/handlers/upload.py | 7 | ||||
-rw-r--r-- | app/utils/upload.py | 34 |
2 files changed, 32 insertions, 9 deletions
diff --git a/app/handlers/upload.py b/app/handlers/upload.py index 40f82e5..dfb02bd 100644 --- a/app/handlers/upload.py +++ b/app/handlers/upload.py @@ -105,7 +105,8 @@ class UploadHandler(hbase.BaseHandler): response.reason = ex.log_message if path and utils.upload.is_valid_dir_path(path): - ret_val, reason = utils.upload.create_upload_dir(path) + ret_val, reason = \ + utils.upload.check_or_create_upload_dir(path) if ret_val == 200: response = self._save_files(path) else: @@ -120,7 +121,7 @@ class UploadHandler(hbase.BaseHandler): "%s: %s" % ( self._get_status_message(valid_request), - "Use %s as content type" % self.content_type + "Use %s the content type" % self.content_type ) ) else: @@ -134,7 +135,7 @@ class UploadHandler(hbase.BaseHandler): if self.request.files: response.result = [ - utils.upload.create_or_update_path( + utils.upload.create_or_update_file( path, u_file[0]["filename"], u_file[0]["content_type"], diff --git a/app/utils/upload.py b/app/utils/upload.py index 67026c9..6841174 100644 --- a/app/utils/upload.py +++ b/app/utils/upload.py @@ -20,7 +20,7 @@ import utils def is_valid_dir_path(path): - """Verify if the provided path is valid as a directory. + """Verify if the provided path is a valid directory. A valid path must be a directory or it does not have to exists. @@ -36,23 +36,45 @@ def is_valid_dir_path(path): return is_valid -def create_upload_dir(path): +def check_or_create_upload_dir(path): + """Check if the path exists and it can be accessed, or create it. + + :param path: The path to verify. + :type path: str + :return A tuple with status code and error. Status code will be 200 in case + it is OK, 500 in case of error. + """ ret_val = 200 error = None real_path = os.path.join(utils.BASE_PATH, path) - if not os.path.exists(real_path): + if os.path.exists(real_path): + os.access(real_path, os.R_OK | os.W_OK | os.X_OK) + else: try: - os.makedirs(real_path) + os.makedirs(real_path, mode=0775) except OSError, ex: utils.LOG.exception(ex) ret_val = 500 - error = "Unable to access destination directory '%s'" % path + error = "Unable to create destination directory '%s'" % path return ret_val, error -def create_or_update_path(path, filename, content_type, content): +def create_or_update_file(path, filename, content_type, content): + """Create or replace a file. + + :param path: The path where the file should be saved. + :type path: str + :param filename: The name of the file to save. + :type filename: str + :param content_type: The media type of the file. + :type content_type: str + :param content: The content of the file. + :type content: str + :return A dictionary that contains the status code of the operation, an + error string if it occurred, the bytes written and the file name. + """ ret_dict = { "status": 200, "error": None, |