Skip to content

Commit

Permalink
solved tsmlo error
Browse files Browse the repository at this point in the history
  • Loading branch information
neerajdarwin committed May 2, 2024
1 parent 2676517 commit bba0e88
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ def import_resize_update_tags_from_project_application():

# Resizes the photos
bucket_name_resized = "processed"
sizes_type = ["T", "S", "L"]
sizes_type = ["T", "S", "M", "L", "O"]
media_type = "Photos"
resizer = Resizer(bucket_name_resized, sizes_type, media_type)
resizer.resize_media()

# Resizes the videos
sizes_type = ["S", "L"]
sizes_type = ["T", "S", "L"]
media_type = "Videos"
resizer = Resizer(bucket_name_resized, sizes_type, media_type)
resizer.resize_media()
Expand Down
77 changes: 41 additions & 36 deletions SpiMediaGallery/main/management/commands/resize_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,49 +190,51 @@ def resize_media(self):

for medium in media_to_be_resized:
# Download Media file from the bucket
print("Will resize:", medium.file.object_storage_key)

suffix = utils.get_file_extension(medium.file.object_storage_key)
medium_file = tempfile.NamedTemporaryFile(suffix="." + suffix, delete=False)
medium_file.close()
start_download = time.time()

try:
self._download_medium(medium, medium_file.name)
except ClientError:
print("Cannot download: {}".format(medium.file.object_storage_key))
continue

download_time = time.time() - start_download

assert os.stat(medium_file.name).st_size == medium.file.size

if verbose:
speed = (medium.file.size / 1024 / 1024) / download_time # MB/s
print(
"Download Stats. Size: {} Time: {} Speed: {:.2f} MB/s File: {}".format(
utils.bytes_to_human_readable(medium.file.size),
utils.seconds_to_human_readable(download_time),
speed,
medium.file.object_storage_key,
if medium.file is not None:
print("Will resize:", medium.file.object_storage_key)
suffix = utils.get_file_extension(medium.file.object_storage_key)
medium_file = tempfile.NamedTemporaryFile(suffix="." + suffix, delete=False)
medium_file.close()
start_download = time.time()
try:
self._download_medium(medium, medium_file.name)
except ClientError:
print("Cannot download: {}".format(medium.file.object_storage_key))
continue

download_time = time.time() - start_download

assert os.stat(medium_file.name).st_size == medium.file.size

if verbose:
speed = (medium.file.size / 1024 / 1024) / download_time # MB/s
print(
"Download Stats. Size: {} Time: {} Speed: {:.2f} MB/s File: {}".format(
utils.bytes_to_human_readable(medium.file.size),
utils.seconds_to_human_readable(download_time),
speed,
medium.file.object_storage_key,
)
)
)

if medium.file.md5 is None:
md5_media_file = utils.hash_of_file_path(medium_file.name)
medium.file.md5 = md5_media_file
medium.file.save()
if medium.file.md5 is None:
md5_media_file = utils.hash_of_file_path(medium_file.name)
medium.file.md5 = md5_media_file
medium.file.save()

self._resize_medium(medium, medium_file.name, self._sizes_type)
self._resize_medium(medium, medium_file.name, self._sizes_type)

print("Finished: medium.id: {}".format(medium.id))
print("Finished: medium.id: {}".format(medium.id))

os.remove(medium_file.name)
os.remove(medium_file.name)

if self._medium_type == Medium.PHOTO:
progress_report.increment_and_print_if_needed()
if self._medium_type == Medium.PHOTO:
progress_report.increment_and_print_if_needed()
else:
progress_report.increment_steps_and_print_if_needed(medium.file.size)
else:
progress_report.increment_steps_and_print_if_needed(medium.file.size)
print("Medium file is None. Skipping this medium.")


# @transaction.atomic
# Getting some:
Expand Down Expand Up @@ -309,6 +311,9 @@ def _resize_medium(self, medium, medium_local_file, sizes: List[str]) -> None:
os.remove(file_to_delete)

def _download_medium(self, medium, download_path):
if medium.file is None:
print("Medium file is None. Unable to download.")
return
bucket_name = medium.file.bucket_name()

if bucket_name not in self._media_buckets:
Expand Down

0 comments on commit bba0e88

Please sign in to comment.