From ab35aed334297b4e5f0652fae37cbadd9de78821 Mon Sep 17 00:00:00 2001 From: Oscar Hudson Date: Sun, 2 Jun 2024 03:32:40 +0100 Subject: [PATCH 1/7] add ds mov uploading? --- theunderground/forms.py | 1 + theunderground/movies.py | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/theunderground/forms.py b/theunderground/forms.py index 33340001..a9f65892 100644 --- a/theunderground/forms.py +++ b/theunderground/forms.py @@ -72,6 +72,7 @@ def validate_new_password(self, _): class MovieUploadForm(FlaskForm): movie = FileField("Movie", validators=[FileRequired()]) + dsmovie = FileField("DS Movie") title = StringField("Movie title", validators=[DataRequired(), Length(max=48)]) thumbnail = FileField("Movie thumbnail", validators=[FileRequired()]) # Choices for the select field are only evaluated once, so we must set it when necessary. diff --git a/theunderground/movies.py b/theunderground/movies.py index 1f88217a..96217951 100644 --- a/theunderground/movies.py +++ b/theunderground/movies.py @@ -54,11 +54,20 @@ def add_movie(): if form.validate_on_submit(): movie = form.movie.data + dsmovie = form.dsmovie.data thumbnail = form.thumbnail.data if movie and thumbnail: movie_data = movie[0].read() thumbnail_data = thumbnail[0].read() + if dsmovie: + dsmovie_data = dsmovie[0].read() + ds_mov_id = 2 + genre = 1 + else: + ds_mov_id = None + genre = 0 + if validate_mobiclip(movie_data): # Get the Mobiclip's length from header. length = get_mobiclip_length(movie_data) @@ -70,8 +79,9 @@ def add_movie(): category_id=form.category.data, length=length, aspect=True, - genre=0, + genre=genre, sp_page_id=0, + ds_mov_id=ds_mov_id, staff=False, ) @@ -81,6 +91,16 @@ def add_movie(): # Now that we've inserted the movie, we can properly move it. save_movie_data(db_movie.movie_id, thumbnail_data, movie_data) + # Save the DS movie if it exists to /dsmov/{get_movie_path}/{movie_id}.enc + # NOT on s3 + + if dsmovie: + dsmovie_dir = get_movie_path(db_movie.movie_id) + dsmovie_path = f"{dsmovie_dir}/{db_movie.movie_id}.enc" + + with open(dsmovie_path, "wb") as f: + f.write(dsmovie_data) + # Finally update the category if needed by S3 if s3: cat_xml = list_category_search(form.category.data) From 46a26781664e78bc87051cc7f1259116b7f75de4 Mon Sep 17 00:00:00 2001 From: Oscar Hudson Date: Sun, 2 Jun 2024 04:34:06 +0100 Subject: [PATCH 2/7] resolve db entry (forgot null default) --- theunderground/movies.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/theunderground/movies.py b/theunderground/movies.py index 96217951..95b9d34a 100644 --- a/theunderground/movies.py +++ b/theunderground/movies.py @@ -62,10 +62,8 @@ def add_movie(): if dsmovie: dsmovie_data = dsmovie[0].read() - ds_mov_id = 2 genre = 1 else: - ds_mov_id = None genre = 0 if validate_mobiclip(movie_data): @@ -81,10 +79,12 @@ def add_movie(): aspect=True, genre=genre, sp_page_id=0, - ds_mov_id=ds_mov_id, staff=False, ) + if dsmovie: + db_movie.ds_mov_id = db_movie.movie_id + db.session.add(db_movie) db.session.commit() From 0a9e6f3a85e2afa6433ac495cf4647d4c1630917 Mon Sep 17 00:00:00 2001 From: Oscar Hudson Date: Sun, 2 Jun 2024 04:52:56 +0100 Subject: [PATCH 3/7] mobi DSi validation --- theunderground/mobiclip.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/theunderground/mobiclip.py b/theunderground/mobiclip.py index 5e04d24b..d5bde729 100644 --- a/theunderground/mobiclip.py +++ b/theunderground/mobiclip.py @@ -54,6 +54,19 @@ def validate_mobiclip(file_data: bytes) -> bool: return True +def validate_mobi_dsi(file_data: bytes) -> bool: + # Validate file magic + if file_data[0:4] == b"MODS": + pass + elif file_data[0:4] == b"SSCF": + # Check for NINTENDO header + if file_data[17:25] != b"NINTENDO": + return False + else: + return False + + return True + def get_category_list(): db_categories = Categories.query.all() From a24791670dcde8926a2b03b1c1f30bfb91716a51 Mon Sep 17 00:00:00 2001 From: Oscar Hudson Date: Sun, 2 Jun 2024 04:58:25 +0100 Subject: [PATCH 4/7] add get_ds_movie_path --- theunderground/mobiclip.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/theunderground/mobiclip.py b/theunderground/mobiclip.py index d5bde729..d393411e 100644 --- a/theunderground/mobiclip.py +++ b/theunderground/mobiclip.py @@ -36,6 +36,13 @@ def get_movie_path(movie_id: int) -> str: return f"movie/{movie_byte}" else: return f"./assets/movies/{movie_byte}" + +def get_ds_movie_path(movie_id: int) -> str: + movie_byte = get_movie_byte(movie_id) + if s3: + return f"dsmov/{movie_byte}" + else: + return f"./assets/dsmov/{movie_byte}" def get_pay_movie_dir(movie_id: int) -> str: @@ -105,8 +112,9 @@ def get_mobiclip_length(file_data: bytes) -> str: return strftime("%H:%M:%S", gmtime(length)) -def save_movie_data(movie_id: int, thumbnail_data: bytes, movie_data: bytes): +def save_movie_data(movie_id: int, thumbnail_data: bytes, movie_data: bytes, ds_movie_data: bytes = None): movie_dir = get_movie_path(movie_id) + ds_movie_dir = get_ds_movie_path(movie_id) md5_hash = get_movie_byte(movie_id) if s3: @@ -125,6 +133,11 @@ def save_movie_data(movie_id: int, thumbnail_data: bytes, movie_data: bytes): s3.upload_fileobj( BytesIO(thumbnail_data), config.r2_bucket_name, thumbnail_path ) + + if ds_movie_data: + # Upload DS movie + ds_movie_path = f"{ds_movie_dir}/{movie_id}.enc" + s3.upload_fileobj(BytesIO(ds_movie_data), config.r2_bucket_name, ds_movie_path) else: if not os.path.isdir(movie_dir): os.makedirs(movie_dir) From c7fa77380dcbb13afecbd26511119fd81f78b9ed Mon Sep 17 00:00:00 2001 From: Oscar Hudson Date: Sun, 2 Jun 2024 04:59:26 +0100 Subject: [PATCH 5/7] actually upload --- theunderground/movies.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/theunderground/movies.py b/theunderground/movies.py index 95b9d34a..aef833a1 100644 --- a/theunderground/movies.py +++ b/theunderground/movies.py @@ -13,6 +13,7 @@ from theunderground.mobiclip import ( get_category_list, validate_mobiclip, + validate_mobi_dsi, get_mobiclip_length, save_movie_data, delete_movie_data, @@ -62,9 +63,11 @@ def add_movie(): if dsmovie: dsmovie_data = dsmovie[0].read() + validation_ds = validate_mobi_dsi(dsmovie_data) genre = 1 else: genre = 0 + validation_ds = False if validate_mobiclip(movie_data): # Get the Mobiclip's length from header. @@ -82,24 +85,14 @@ def add_movie(): staff=False, ) - if dsmovie: + if dsmovie and validation_ds: db_movie.ds_mov_id = db_movie.movie_id db.session.add(db_movie) db.session.commit() # Now that we've inserted the movie, we can properly move it. - save_movie_data(db_movie.movie_id, thumbnail_data, movie_data) - - # Save the DS movie if it exists to /dsmov/{get_movie_path}/{movie_id}.enc - # NOT on s3 - - if dsmovie: - dsmovie_dir = get_movie_path(db_movie.movie_id) - dsmovie_path = f"{dsmovie_dir}/{db_movie.movie_id}.enc" - - with open(dsmovie_path, "wb") as f: - f.write(dsmovie_data) + save_movie_data(db_movie.movie_id, thumbnail_data, movie_data, dsmovie_data if dsmovie and validation_ds else None) # Finally update the category if needed by S3 if s3: From 807eea27ed4b9ee36024f54ed4292be0d76ff220 Mon Sep 17 00:00:00 2001 From: Oscar Hudson Date: Sun, 2 Jun 2024 05:04:39 +0100 Subject: [PATCH 6/7] save DS movie if not s3 --- theunderground/mobiclip.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/theunderground/mobiclip.py b/theunderground/mobiclip.py index d393411e..6fc113df 100644 --- a/theunderground/mobiclip.py +++ b/theunderground/mobiclip.py @@ -153,6 +153,15 @@ def save_movie_data(movie_id: int, thumbnail_data: bytes, movie_data: bytes, ds_ movie.write(movie_data) movie.close() + if ds_movie_data: + if not os.path.isdir(ds_movie_dir): + os.makedirs(ds_movie_dir) + + # Write DS movie + ds_movie = open(f"{ds_movie_dir}/{movie_id}.enc", "wb") + ds_movie.write(ds_movie_data) + ds_movie.close() + def save_pay_movie_data( movie_id: int, thumbnail_data: bytes, movie_data: bytes, poster_data: bytes From 1695414c38c58e0ff013535e6ece85cd31ad5212 Mon Sep 17 00:00:00 2001 From: Oscar Hudson Date: Mon, 3 Jun 2024 00:25:37 +0100 Subject: [PATCH 7/7] make it prettier --- theunderground/movies.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/theunderground/movies.py b/theunderground/movies.py index aef833a1..7d5834df 100644 --- a/theunderground/movies.py +++ b/theunderground/movies.py @@ -63,9 +63,10 @@ def add_movie(): if dsmovie: dsmovie_data = dsmovie[0].read() - validation_ds = validate_mobi_dsi(dsmovie_data) genre = 1 + validation_ds = validate_mobi_dsi(dsmovie_data) else: + dsmovie_data = None genre = 0 validation_ds = False @@ -92,7 +93,7 @@ def add_movie(): db.session.commit() # Now that we've inserted the movie, we can properly move it. - save_movie_data(db_movie.movie_id, thumbnail_data, movie_data, dsmovie_data if dsmovie and validation_ds else None) + save_movie_data(db_movie.movie_id, thumbnail_data, movie_data, dsmovie_data) # Finally update the category if needed by S3 if s3: