예제 #1
0
def reallydeletefile():
    user_file_id = request.args.get('id')
    temp = user_file_id
    try:
        user_file_id = int(user_file_id)
    except:
        return redirect(
            url_for('recycle_bin',
                    status_msg=f"file with id = `{temp}` did not found"))

    user_files = userFileList()
    query_data = {
        'user_id': int(session['user']['id']),
        'file_id': int(user_file_id)
    }
    this_user_file = user_files.getByFields(query_data)
    print(this_user_file)
    if len(this_user_file) == 0:
        return redirect(
            url_for('recycle_bin',
                    status_msg=f"file with id = `{temp}` did not found"))

    if this_user_file[0]['is_deleted'] == 0:
        return redirect(
            url_for(
                'recycle_bin',
                status_msg=f"recycle bin file with {user_file_id} did not found"
            ))

    user_files.deleteById(user_file_id)

    return redirect(
        url_for('recycle_bin',
                status_msg=f"delete file with id = `{user_file_id}` succeed"))
예제 #2
0
def delete_file():
    user_file_id = request.args.get('id')
    temp = user_file_id
    try:
        user_file_id = int(user_file_id)
    except:
        return redirect(
            url_for('main',
                    status_msg=f"file with id = `{temp}` did not found"))

    user_files = userFileList()
    query_data = {
        'user_id': int(session['user']['id']),
        'file_id': int(user_file_id)
    }
    this_user_file = user_files.getByFields(query_data)
    print(this_user_file)
    if len(this_user_file) == 0:
        return redirect(
            url_for('main',
                    status_msg=f"file with id = `{temp}` did not found"))
    data = {'is_deleted': 1, 'id': user_file_id}
    user_files.update(data)

    return redirect(
        url_for('main',
                status_msg=f"delete file with id = `{user_file_id}` succeed"))
예제 #3
0
def download_file():
    user_file_id = request.args.get('id')
    temp = user_file_id
    try:
        user_file_id = int(user_file_id)
    except:
        return redirect(
            url_for('main',
                    status_msg=f"file with id = `{temp}` did not found"))

    user_files = userFileList()
    query_data = {
        'user_id': int(session['user']['id']),
        'file_id': int(user_file_id)
    }
    this_user_file = user_files.getByFields(query_data)
    print(this_user_file)
    if len(this_user_file) == 0:
        return redirect(
            url_for('main',
                    status_msg=f"file with id = `{temp}` did not found"))

    filename = this_user_file[0]['filename']
    file_id = this_user_file[0]['file_id']
    files = fileList()
    this_file = files.getById(file_id)
    file_storage_name = this_file[0]['storage_name']
    file_abs_path = os.path.join(config.UPLOAD_FOLDER, file_storage_name)
    return send_file(file_abs_path,
                     as_attachment=True,
                     attachment_filename=filename)
예제 #4
0
def sharefile():
    user_file_id = request.args.get('id')
    temp = user_file_id
    try:
        user_file_id = int(user_file_id)
    except:
        return redirect(
            url_for('main',
                    status_msg=f"file with id = `{temp}` did not found"))

    user_files = userFileList()
    query_data = {
        'user_id': int(session['user']['id']),
        'file_id': int(user_file_id)
    }
    this_user_file = user_files.getByFields(query_data)
    print(this_user_file)
    if len(this_user_file) == 0:
        return redirect(
            url_for('main',
                    status_msg=f"file with id = `{temp}` did not found"))

    if this_user_file[0]['is_deleted'] == 1:
        return redirect(
            url_for('main',
                    status_msg=f"file with {user_file_id} did not found"))

    shared_files = sharedFileList()
    share_id = util.gen_share_id()
    access_token = util.gen_access_token()

    data = {
        'id': share_id,
        'access_token': access_token,
        'user_id': query_data['user_id'],
        'user_file_id': user_file_id,
        'filename': this_user_file[0]['filename']
    }
    shared_files.insert(data)

    data = {'is_shared': 1, 'id': user_file_id}
    user_files.update(data)
    return redirect(
        url_for(
            'main',
            status_msg=
            f"share file with id = `{user_file_id}` succeed, the share link is {config.share_link_prefix+share_id} the access token is {access_token}"
        ))
예제 #5
0
def main():
    status_msg = request.args.get('status_msg')
    user_id = session['user']['id']
    user_files = userFileList()
    this_user_files = user_files.getByField('user_id', user_id)
    this_user_files = [
        this_file for this_file in this_user_files
        if this_file['is_deleted'] == 0
    ]
    for file in this_user_files:
        if file['is_shared'] == 0:
            file['is_shared'] = "No"
        else:
            file['is_shared'] = "Yes"

    userinfo = 'Hello, ' + session['user']['fname']
    return render_template('main.html',
                           title='Main menu',
                           msg=userinfo,
                           files=this_user_files,
                           status_msg=status_msg)
예제 #6
0
def upload_file():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit an empty part without filename
        if file.filename == '':
            flash('No selected file')
            #return redirect(request.url)
            return render_template('uploadfile.html',
                                   title='upload file',
                                   status_msg='Please select a file !')

        filename = secure_filename(file.filename)
        user_id = session['user']['id']
        files = fileList()
        user_files = userFileList()
        _, ext = util.split_filename(filename)
        file_storage_name = util.gen_uuid_str() + ext
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], file_storage_name))

        file_data = {"storage_name": file_storage_name}
        file_id = files.insert(file_data)

        user_file_data = {
            'user_id': user_id,
            "file_id": file_id,
            "filename": filename
        }
        user_files.insert(user_file_data)
        return render_template('uploadfile.html',
                               title='upload success',
                               status_msg=filename + ' upload success !')
    return render_template('uploadfile.html', title='upload file')
예제 #7
0
def handle_share(share_id):
    if checkSession2() and request.args.get('action') == 'download':
        shared_files = sharedFileList()
        user_files = userFileList()
        this_shared_file = shared_files.getById(share_id)
        this_user_file = user_files.getById(
            this_shared_file[0]['user_file_id'])
        filename = this_shared_file[0]['filename']
        file_id = this_user_file[0]['file_id']
        user_files = userFileList()

        files = fileList()
        this_file = files.getById(file_id)
        file_storage_name = this_file[0]['storage_name']
        file_abs_path = os.path.join(config.UPLOAD_FOLDER, file_storage_name)
        return send_file(file_abs_path,
                         as_attachment=True,
                         attachment_filename=filename)

    shared_files = sharedFileList()
    if request.method == 'POST':
        token = request.form.get('token')
        this_shared_file = shared_files.getById(share_id)
        if len(this_shared_file) == 0:
            return render_template(
                'share.html',
                status_msg=f"share with share_id = `{share_id}` does not exist"
            )

        if this_shared_file[0]['access_token'] != token:
            return render_template('share.html',
                                   status_msg=f"token incorrect",
                                   filename=this_shared_file[0]['filename'],
                                   share_exist=True,
                                   link=f"/s/{share_id}")

        user_files = userFileList()
        this_user_file = user_files.getById(
            this_shared_file[0]['user_file_id'])
        if len(this_user_file) == 0 or this_user_file[0][
                'is_deleted'] == 1 or this_user_file[0]['is_shared'] == 0:
            return render_template(
                'share.html',
                status_msg=f"share with share_id = `{share_id}` does not exist"
            )
        session['active'] = time.time()
        session['verified'] = True

        return render_template('share.html',
                               download_link=f"/s/{share_id}?action=download",
                               filename=this_shared_file[0]['filename'],
                               share_exist=True,
                               link=f"/s/{share_id}")
        """
        filename = this_shared_file[0]['filename']
        file_id = this_user_file[0]['file_id']
        files = fileList()
        this_file = files.getById(file_id)
        file_storage_name = this_file[0]['storage_name']
        file_abs_path = os.path.join(config.UPLOAD_FOLDER, file_storage_name)
        return send_file(file_abs_path,
                         as_attachment=True,
                         attachment_filename=filename)
        """

    this_shared_file = shared_files.getById(share_id)
    if len(this_shared_file) == 0:
        return render_template(
            'share.html',
            status_msg=f"share with share_id = `{share_id}` does not exist")
    user_files = userFileList()
    this_user_file = user_files.getById(this_shared_file[0]['user_file_id'])
    if len(this_user_file) == 0 or this_user_file[0][
            'is_deleted'] == 1 or this_user_file[0]['is_shared'] == 0:
        return render_template(
            'share.html',
            status_msg=f"share with share_id = `{share_id}` does not exist")

    return render_template(
        'share.html',
        share_exist=True,
        filename=this_shared_file[0]['filename'],
        link=f"/s/{share_id}")  #filename=this_shared_file[0]['filename']