create new File model for better Directory and file thumbnail state management

This commit is contained in:
Xevion
2020-10-31 20:37:31 -05:00
parent ff96bf4753
commit 02c58830f7
3 changed files with 98 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
# Generated by Django 3.1.2 on 2020-11-01 01:07
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('viewer', '0002_auto_20201031_0526'),
]
operations = [
migrations.AlterField(
model_name='serveddirectory',
name='regex_pattern',
field=models.CharField(default='', max_length=100, verbose_name='RegEx Matching Pattern'),
),
]

View File

@@ -0,0 +1,24 @@
# Generated by Django 3.1.2 on 2020-11-01 01:36
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('viewer', '0003_auto_20201031_2007'),
]
operations = [
migrations.CreateModel(
name='File',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('path', models.CharField(max_length=300, verbose_name='Full Filepath')),
('filename', models.CharField(max_length=160, verbose_name='Filename')),
('mediatype', models.CharField(max_length=30, verbose_name='Mediatype')),
('directory', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='files', to='viewer.serveddirectory')),
],
),
]

View File

@@ -1,6 +1,13 @@
import mimetypes
import os
import uuid
from django.db import models
from django.urls import reverse
from easy_thumbnails.alias import aliases
if not aliases.get('small'):
aliases.set('small', {'size': (150, 80), 'crop': True})
class ServedDirectory(models.Model):
@@ -20,5 +27,54 @@ class ServedDirectory(models.Model):
regex = models.BooleanField('Directory RegEx Option', default=False)
match_filename = models.BooleanField('RegEx Matches Against Filename', default=True)
def refresh(self):
"""Refresh the directory listing to see if any new files have appeared and add them to the list."""
for file in os.listdir(self.path):
# Check if the file has been entered before
entry = self.files.filter(filename__exact=file).first()
if entry is None:
# create the file entry
entry = File.create(full_path=os.path.join(self.path, file))
entry.save()
def __str__(self):
return self.path
class File(models.Model):
path = models.CharField('Full Filepath', max_length=300)
filename = models.CharField('Filename', max_length=160)
mediatype = models.CharField('Mediatype', max_length=30)
directory = models.ForeignKey(ServedDirectory, on_delete=models.CASCADE, related_name='files')
@classmethod
def create(cls, full_path: str) -> 'File':
"""Simple shortcut for creating a File database entry with just the path."""
return File(
path=full_path,
filename=os.path.basename(full_path),
mediatype=File.get_mediatype(full_path),
directory=os.path.dirname(full_path)
)
def get_url(self, directory: ServedDirectory) -> str:
"""Retrieve the direct URL for a given file."""
return reverse('file', args=(directory.id, self.filename))
@staticmethod
def get_mediatype(path) -> str:
"""Simple media type categorization based on the given path."""
if os.path.exists(path):
if os.path.isdir(path):
return 'folder'
mimetype = mimetypes.guess_type(path)[0]
if mimetype is not None:
if mimetype.startswith('image'):
return 'image'
elif mimetype.startswith('video'):
return 'video'
return 'file'
return 'unknown'
def __str__(self) -> str:
return self.filename