Exemple #1
0
def upload(directory: str, file: object, rename: bool, *allow_extensions):
    """
    file-upload interface, given a folder and a file from request, then saving it!

    @Args:
    * [directory] str
    * [file] request.files
    * [rename] bool
    * [allow_extensions] list of str

    @Returns:
    * [bool] if file saved, return true, else false
    * [str] if success, return the filename, else the failure message
    """

    # folder not exists, create one
    if not FileUtils.exists(directory):
        FileUtils.mkdir(directory)

    if not file:
        return False, "file is null"

    if allowed_ext(file.filename, allow_extensions):

        if rename:
            success_name = rename_file(file.filename)
            file.save(os.path.join(directory, success_name))
        else:
            success_name = file.filename
            file.save(os.path.join(directory, success_name))

        return True, success_name

    return False, "file extension is not allowed"
Exemple #2
0
 def __init__(self, b_stdout=True, b_log=False, directory=None, file=None):
     self.m_bStdOut = b_stdout
     self.m_bUseLog = b_log
     if self.m_bUseLog:
         FileUtils.mkdir(directory)
         self.m_file = file
         self.m_dir = directory