Esempio n. 1
0
def upload(name):
    """
	This page allows a user to upload a text or image file.
	"""
    error = valid_user(name)
    if error is None:
        if request.method == 'POST':
            file = request.files['file']
            if file and valid_file(file.filename):
                # Sanitize the filename, save the file to the uploads
                # folder, and add the file and owner info to the file database.
                filename = secure_filename(file.filename)
                file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
                file_instance = Upload(name, filename)

                flash('File was uploaded successfully.')
                return redirect(url_for('files', name=name))
            else:
                flash("Invalid filename or file type.")
        return render_template('upload.html')

    # If an error occurs, display the error and
    # redirect to the appropriate page.
    display(error)
    if 'logged_in' in session:
        return redirect(url_for('upload', name=session['logged_in']))
    else:
        return redirect(url_for('login'))
Esempio n. 2
0
def upload(name):
	"""
	This page allows a user to upload a text or image file.
	"""
	error = valid_user(name)
	if error is None:
		if request.method == 'POST':
			file = request.files['file']
			if file and valid_file(file.filename):
				# Sanitize the filename, save the file to the uploads
				# folder, and add the file and owner info to the file database.
				filename = secure_filename(file.filename)
				file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
				file_instance = Upload(name, filename)

				flash('File was uploaded successfully.')
				return redirect(url_for('files', name=name))
			else:
				flash("Invalid filename or file type.")
		return render_template('upload.html')

	# If an error occurs, display the error and
	# redirect to the appropriate page.
	display(error)
	if 'logged_in' in session:
		return redirect(url_for('upload', name=session['logged_in']))
	else:
		return redirect(url_for('login'))
Esempio n. 3
0
def upload(name):
    """
    This page allows a user to upload a text or image file.
    """

    # Refuse access if posting is disabled for the user.
    if "posting_enabled" in session and session['posting_enabled'] == False:
        error = "Access denied."
        display(error)
        if 'logged_in' in session:
            return redirect(url_for('entries', name=session['logged_in']))
        else:
            return redirect(url_for('login'))

    # Check if the user is logged in before allowing to upload files.
    error = valid_user(name)
    if error is None:
        if request.method == 'POST':
            file = request.files['file']
            if file and valid_file(file.filename):
                # Sanitize the filename, save the file to the uploads
                # folder, and add the file and owner info to the file database.
                old_filename = filename = secure_filename(file.filename)
                filetype = filename.rsplit('.', 1)[1].lower()

                # Prevents duplicate filenames by appending (1), (2), etc.
                # e.g. if two "images.jpg" are uploaded, the second one would
                # become "images(1).jpg".
                x = 0
                while os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'], filename)):
                    x += 1
                    filename = ("%s(%s).%s" % (old_filename.rsplit('.', 1)[0], x, filetype))

                # Save the file to the uploads folder.
                file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
                file_instance = Upload(name, filename, filetype)

                # Insert the upload object into the database.
                fdb.session.add(file_instance)
                fdb.session.commit()

                flash('File was uploaded successfully.')
                return redirect(url_for('entries', name=name))
            else:
                flash("Invalid filename or file type.")
        return render_template('upload.html', username=name, theme=session['theme'])

    # If an error occurs, display the error and
    # redirect to the appropriate page.
    display(error)
    if 'logged_in' in session:
        return redirect(url_for('upload', name=session['logged_in']))
    else:
        return redirect(url_for('login'))