84 lines
2.2 KiB
Python
84 lines
2.2 KiB
Python
|
import os
|
||
|
import os.path
|
||
|
import stat
|
||
|
import json
|
||
|
from datetime import datetime
|
||
|
|
||
|
from django.db import models
|
||
|
|
||
|
|
||
|
class Stat:
|
||
|
stat_result: os.stat_result
|
||
|
|
||
|
def __init__(self, stat_result):
|
||
|
self.stat_result = stat_result
|
||
|
|
||
|
def __dict__(self):
|
||
|
return {
|
||
|
"mode": self.mode,
|
||
|
"access_time": self.access_time,
|
||
|
"modified_time": self.modified_time,
|
||
|
"creation_time": self.creation_time,
|
||
|
"birth_time": self.birth_time,
|
||
|
}
|
||
|
|
||
|
def as_dict(self):
|
||
|
return self.__dict__()
|
||
|
|
||
|
@property
|
||
|
def mode(self):
|
||
|
return self.stat_result["st_mode"]
|
||
|
|
||
|
@property
|
||
|
def access_time(self):
|
||
|
return datetime.fromtimestamp(self.stat_result["st_atime"])
|
||
|
|
||
|
@property
|
||
|
def modified_time(self):
|
||
|
return datetime.fromtimestamp(self.stat_result["st_mtime"])
|
||
|
|
||
|
@property
|
||
|
def creation_time(self):
|
||
|
return datetime.fromtimestamp(self.stat_result["st_ctime"])
|
||
|
|
||
|
@property
|
||
|
def birth_time(self):
|
||
|
if "st_birthtime" in self.stat_result:
|
||
|
return datetime.fromtimestamp(self.stat_result["st_birthtime"])
|
||
|
return None
|
||
|
|
||
|
|
||
|
class Picture(models.Model):
|
||
|
PICTURE = "picture"
|
||
|
# PICTURE_360 = 'picture-360'
|
||
|
# PANORAMA = 'picture-panorama'
|
||
|
VIDEO = "video"
|
||
|
# VIDEO_360 = 'video-360'
|
||
|
KIND_CHOICES = (
|
||
|
(PICTURE, PICTURE.capitalize()),
|
||
|
# (PICTURE_360, "Picture 360"),
|
||
|
# (PANORAMA, PANORAMA.capitalize()),
|
||
|
(VIDEO, VIDEO.capitalize()),
|
||
|
# (VIDEO_360, "Video 360"),
|
||
|
)
|
||
|
|
||
|
file_path = models.FilePathField(max_length=255)
|
||
|
creation_date = models.DateTimeField(db_index=True)
|
||
|
checksum = models.CharField(max_length=64)
|
||
|
metadata = models.TextField(blank=True, null=True)
|
||
|
exif = models.TextField(blank=True, null=True)
|
||
|
stat = models.TextField(blank=True, null=True)
|
||
|
mimetype = models.CharField(max_length=64)
|
||
|
raw = models.BooleanField(default=False)
|
||
|
kind = models.CharField(choices=KIND_CHOICES, default=PICTURE, max_length=24)
|
||
|
|
||
|
@property
|
||
|
def filename(self):
|
||
|
return os.path.basename(self.file_path)
|
||
|
|
||
|
def get_stat(self):
|
||
|
return Stat(json.loads(self.stat))
|
||
|
|
||
|
# class Meta:
|
||
|
# ordering = ("creation_date", )
|