Ejemplo n.º 1
0
def avatar():
    avatar = Content.create(user=1,
                            path="/some/path",
                            mime="image/jpeg",
                            size=1)

    from src.model import db

    db.db_wrapper.database.close()

    return avatar
Ejemplo n.º 2
0
def upload():
    """Загрузить файл"""

    import magic

    if "file" not in request.files:
        return errors.content_no_file()

    uploaded_file = request.files["file"]
    if uploaded_file.filename == "":
        return errors.content_no_file()

    if not allowed_file(uploaded_file.filename):
        return errors.content_forbidden_extension()

    file_content = uploaded_file.read(MAX_FILE_SIZE + 1)

    size = len(file_content)
    if size > MAX_FILE_SIZE:
        return errors.content_file_size_exceeded()

    uploaded_file.seek(0)
    name = hashlib.md5(uploaded_file.read()).hexdigest()
    uploaded_file.seek(0)

    _, ext = ntpath.splitext(uploaded_file.filename)

    today = datetime.date.today()

    user = get_user_from_request()

    filename = os.path.join(
        current_app.config["UPLOAD_FOLDER"],
        str(user.id) + "/" + str(today.year) + "/" + str(today.month) + "/",
    )

    os.makedirs(filename, exist_ok=True)

    new_path = filename + name + ext
    uploaded_file.save(new_path)

    full_path = os.path.abspath(new_path)

    mime = magic.from_file(full_path, mime=True)

    content = Content.create(user=user.id,
                             path=full_path,
                             size=size,
                             mime=mime)

    return jsonify({"success": 1, "file": content.to_json()})
Ejemplo n.º 3
0
def upload_image(user_id, year, month, path):
    if path:
        if not os.path.isfile(path):
            print("Can't find file at path " + path)
            return None
        name = md5(path)
        _, ext = ntpath.splitext(path)
        filename = os.path.join(
            "uploads/",
            str(user_id) + "/" + str(year) + "/" + str(month) + "/")
        os.makedirs(filename, exist_ok=True)
        # filename = secure_filename(filename)
        new_path = filename + name + ext

        copyfile(path, new_path)

        c_mime = magic.from_file(new_path, mime=True)
        c_size = os.stat(new_path).st_size

        return Content.create(user=user_id,
                              path=os.path.abspath(new_path),
                              mime=c_mime,
                              size=c_size)
    return None