Add documents to decoders (#22)

* Add decoder documents support
* Use abstract model for Documents
* Increase version
* Code cleanup
This commit is contained in:
2023-10-01 00:03:41 +02:00
committed by GitHub
parent 9483648a1f
commit 5d536ce568
16 changed files with 161 additions and 60 deletions

View File

@@ -1,4 +1,4 @@
from ram.utils import git_suffix
__version__ = "0.3.3"
__version__ = "0.4.0"
__version__ += git_suffix(__file__)

30
ram/ram/models.py Normal file
View File

@@ -0,0 +1,30 @@
import os
from django.db import models
from django.utils.safestring import mark_safe
from ram.utils import DeduplicatedStorage
class Document(models.Model):
description = models.CharField(max_length=128, blank=True)
file = models.FileField(
upload_to="files/",
storage=DeduplicatedStorage(),
null=True,
blank=True,
)
class Meta:
abstract = True
def __str__(self):
return "{0}".format(os.path.basename(self.file.name))
def filename(self):
return self.__str__()
def download(self):
return mark_safe(
'<a href="{0}" target="_blank">Link</a>'.format(self.file.url)
)

View File

@@ -16,7 +16,7 @@ class DeduplicatedStorage(FileSystemStorage):
def save(self, name, content, max_length=None):
if super().exists(name):
new = hashlib.sha256(content.file.getbuffer()).hexdigest()
new = hashlib.sha256(content.read()).hexdigest()
with open(super().path(name), "rb") as file:
file_binary = file.read()
old = hashlib.sha256(file_binary).hexdigest()