Skip to content

Commit

Permalink
s3 is now a storage provider choice for vidzy!
Browse files Browse the repository at this point in the history
  • Loading branch information
ProgramminCat committed Aug 21, 2024
1 parent d93b002 commit 29e2100
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ VIDZY_DB_PASS= ${MYSQL_PASSWORD}
MINIFY_HTML= False
HOST= "0.0.0.0"

S3_ENABLED=False
AWS_ACCESS_KEY_ID=AWS_ACCESS_KEY
AWS_SECRET_ACCESS_KEY=AWS_SECRET_KEY
AWS_ENDPOINT_URL=https://s3.us-east-1.amazonaws.com
S3_BUCKET_NAME=vidzy
33 changes: 26 additions & 7 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import math
import nh3
import vidzyconfig
import boto3
import uuid


CLEANR = re.compile('<.*?>')
Expand Down Expand Up @@ -66,6 +68,9 @@ def cleanhtml(raw_html):

mysql.init_app(app)

s3_enabled = app.config['S3_ENABLED']
print("S3 enabled:", s3_enabled)

@app.template_filter('image_proxy')
def image_proxy(src):
return "/proxy/?url=" + quote(str(src))
Expand Down Expand Up @@ -807,16 +812,30 @@ def upload_file():
return redirect(request.url)
if file and allowed_file(file.filename):
filename = datetime.today().strftime('%Y%m%d') + secure_filename(file.filename)
if vidzyconfig.config["use_absolute_upload_path"]:
project_folder = vidzyconfig.config["vidzy_absolute_path"]
file.save(os.path.join(project_folder + '/' + app.config['UPLOAD_FOLDER'], filename))
if s3_enabled == True:
new_filename = uuid.uuid4().hex + '.' + file.filename.rsplit('.', 1)[1].lower()

bucket_name = app.config['S3_BUCKET_NAME']
s3 = boto3.resource("s3")
s3.Bucket(bucket_name).upload_fileobj(file, new_filename)

s3_fileurl = app.config['AWS_ENDPOINT_URL'] + "/" + app.config['S3_BUCKET_NAME'] + "/" + new_filename

cur = mysql.connection.cursor()

cur.execute( """INSERT INTO shorts (title, url, user_id, date_uploaded) VALUES (%s,%s,%s,%s)""", (request.form.get("title"), s3_fileurl, str(session["user"]["id"]), datetime.now().strftime('%Y-%m-%d')) )
mysql.connection.commit()
else:
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
if vidzyconfig.config["use_absolute_upload_path"]:
project_folder = vidzyconfig.config["vidzy_absolute_path"]
file.save(os.path.join(project_folder + '/' + app.config['UPLOAD_FOLDER'], filename))
else:
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

cur = mysql.connection.cursor()
cur = mysql.connection.cursor()

cur.execute( """INSERT INTO shorts (title, url, user_id, date_uploaded) VALUES (%s,%s,%s,%s)""", (request.form.get("title"), filename, str(session["user"]["id"]), datetime.now().strftime('%Y-%m-%d')) )
mysql.connection.commit()
cur.execute( """INSERT INTO shorts (title, url, user_id, date_uploaded) VALUES (%s,%s,%s,%s)""", (request.form.get("title"), filename, str(session["user"]["id"]), datetime.now().strftime('%Y-%m-%d')) )
mysql.connection.commit()

return redirect(url_for('index_page'))
return '''
Expand Down
8 changes: 7 additions & 1 deletion mysql-dump/MYSQL_DATABASE.sql
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,10 @@ CREATE TABLE `comments` (
ALTER TABLE `comments`
ADD COLUMN `comment_text` VARCHAR(145) NULL AFTER `user_id`;
ALTER TABLE `shorts`
ADD COLUMN `description` VARCHAR(400) NULL AFTER `date_uploaded`;
ADD COLUMN `description` VARCHAR(400) NULL AFTER `date_uploaded`;


--
-- v0.1.4
--
ALTER TABLE `shorts` CHANGE COLUMN `url` `url` VARCHAR(100) NULL DEFAULT NULL ;
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ requests
python-dotenv
cryptography
Flask-WTF
nh3==0.2.18
nh3==0.2.18
boto3
6 changes: 5 additions & 1 deletion settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@
MINIFY_HTML = os.environ.get("MINIFY_HTML")
HOST = os.environ.get("HOST")

ALLOW_UPLOADS = os.environ.get("ALLOW_UPLOADS")
ALLOW_UPLOADS = os.environ.get("ALLOW_UPLOADS")

S3_ENABLED = os.environ.get("S3_ENABLED")
S3_BUCKET_NAME = os.environ.get("S3_BUCKET_NAME")
AWS_ENDPOINT_URL = os.environ.get("AWS_ENDPOINT_URL")
4 changes: 4 additions & 0 deletions templates/displayshort.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<div class="video">
<shortid style="display: none;">{{ short.id }}</shortid>

{% if short.url.startswith('https://') %}
<video class="video__player" src="{{ short.url }}" loop controls></video>
{% else %}
<video class="video__player" src="/static/uploads/{{ short.url }}" loop controls></video>
{% endif %}

<!-- sidebar -->
<div class="videoSidebar">
Expand Down

0 comments on commit 29e2100

Please sign in to comment.