views.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:fileserver 作者: ReCodEx 项目源码 文件源码
def store_submission(id, dirs: DirectoryStructure):
    """
    Store files submitted by a user and create an archive for workers convenience.
    Expects that the body of the POST request uses file paths as keys and the 
    content of the files as values.
    """

    # Make a separate directory for the submitted files
    job_dir = os.path.join(dirs.submission_dir, id)
    os.makedirs(job_dir, exist_ok=True)

    # Save each received file
    for name, content in request.files.items():
        # Get the directory of the file path and create it, if necessary
        dirname = os.path.dirname(name)
        if dirname:
            os.makedirs(os.path.join(job_dir, dirname), exist_ok=True)

        # Save the file
        with open(os.path.join(job_dir, name), 'wb') as f:
            content.save(f)

    # Make an archive that contains the submitted files
    shutil.make_archive(os.path.join(dirs.archive_dir, id), "zip", root_dir=dirs.submission_dir, base_dir=id)

    # Return the path to the archive
    return json.dumps({
        "archive_path": url_for('fileserver.get_submission_archive', id=id, ext='zip'),
        "result_path": url_for('fileserver.store_result', id=id, ext='zip')
    })
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号