mirror of
https://github.com/Xevion/simple-viewer.git
synced 2025-12-06 05:16:35 -06:00
add filesize field with human readable property, rename all size fields to resolution, private __thumbs_path var for ease of use
This commit is contained in:
@@ -4,6 +4,7 @@ import uuid
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
|
|
||||||
|
import humanize
|
||||||
import jsonfield
|
import jsonfield
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
@@ -72,7 +73,7 @@ class ServedDirectory(models.Model):
|
|||||||
return self.path
|
return self.path
|
||||||
|
|
||||||
|
|
||||||
class ImageDimensions(models.Model):
|
class ImageResolution(models.Model):
|
||||||
"""
|
"""
|
||||||
A simple model for storing the dimensions of a specific image. A tuple, in essence.
|
A simple model for storing the dimensions of a specific image. A tuple, in essence.
|
||||||
"""
|
"""
|
||||||
@@ -103,8 +104,9 @@ class File(models.Model):
|
|||||||
|
|
||||||
fileLastModified = models.DateTimeField()
|
fileLastModified = models.DateTimeField()
|
||||||
lastRefreshed = models.DateTimeField()
|
lastRefreshed = models.DateTimeField()
|
||||||
size = models.OneToOneField(ImageDimensions, on_delete=models.CASCADE)
|
size = models.PositiveIntegerField()
|
||||||
thumbnailSize = models.OneToOneField(ImageDimensions, on_delete=models.CASCADE)
|
resolution = models.OneToOneField(ImageResolution, on_delete=models.CASCADE)
|
||||||
|
thumbnailResolution = models.OneToOneField(ImageResolution, on_delete=models.CASCADE)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create(cls, full_path: str, parent: ServedDirectory) -> 'File':
|
def create(cls, full_path: str, parent: ServedDirectory) -> 'File':
|
||||||
@@ -134,8 +136,9 @@ class File(models.Model):
|
|||||||
self.generate_thumbnail()
|
self.generate_thumbnail()
|
||||||
|
|
||||||
if updated:
|
if updated:
|
||||||
self.size.set(helpers.get_resolution())
|
self.resolution.set(helpers.get_resolution(self.path))
|
||||||
self.thumbnailSize.set(helpers.get_resolution())
|
self.thumbnailResolution.set(helpers.get_resolution(self.__thumbs_path))
|
||||||
|
self.size = os.path.getsize(self.path)
|
||||||
|
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
@@ -147,13 +150,17 @@ class File(models.Model):
|
|||||||
"""Delete the thumbnail for this File if it exists and forget the filename."""
|
"""Delete the thumbnail for this File if it exists and forget the filename."""
|
||||||
if self.thumbnail:
|
if self.thumbnail:
|
||||||
try:
|
try:
|
||||||
os.remove(os.path.join(self.thumbs_dir, self.thumbnail))
|
os.remove(self.__thumbs_path)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
finally:
|
finally:
|
||||||
self.thumbnail = None
|
self.thumbnail = None
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def human_size(self) -> str:
|
||||||
|
"""returns a human readable interpretation of the size of this file"""
|
||||||
|
return humanize.naturalsize(self.size)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def thumbnail_static_path(self):
|
def thumbnail_static_path(self):
|
||||||
@@ -161,10 +168,16 @@ class File(models.Model):
|
|||||||
return f'/thumbnails/{self.thumbnail}'
|
return f'/thumbnails/{self.thumbnail}'
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def thumbs_dir(self):
|
def __thumbs_dir(self) -> str:
|
||||||
"""A string path to the directory containing thumbnails."""
|
"""A string path to the directory containing thumbnails."""
|
||||||
return os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static', 'thumbnails')
|
return os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static', 'thumbnails')
|
||||||
|
|
||||||
|
@property
|
||||||
|
def __thumbs_path(self) -> str:
|
||||||
|
"""A string path to the thumbnail file."""
|
||||||
|
if self.thumbnail:
|
||||||
|
return os.path.join(self.__thumbs_dir, self.thumbnail)
|
||||||
|
|
||||||
def generate_thumbnail(self, regenerate=False) -> None:
|
def generate_thumbnail(self, regenerate=False) -> None:
|
||||||
"""
|
"""
|
||||||
Generates a new thumbnail for a given image or video file.
|
Generates a new thumbnail for a given image or video file.
|
||||||
@@ -187,7 +200,7 @@ class File(models.Model):
|
|||||||
|
|
||||||
# Generate thumbnail
|
# Generate thumbnail
|
||||||
try:
|
try:
|
||||||
helpers.generate_thumbnail(self.path, os.path.join(self.thumbs_dir, self.thumbnail))
|
helpers.generate_thumbnail(self.path, self.__thumbs_path)
|
||||||
except Exception:
|
except Exception:
|
||||||
print(f'Could not thumbnail: {self.filename}')
|
print(f'Could not thumbnail: {self.filename}')
|
||||||
self.delete_thumbnail()
|
self.delete_thumbnail()
|
||||||
|
|||||||
Reference in New Issue
Block a user