Fixed mediafiles

This commit is contained in:
FinnStutzenstein 2019-11-08 13:09:07 +01:00
parent acab54738b
commit e416117b11
5 changed files with 68 additions and 11 deletions

View File

@ -54,7 +54,10 @@
<th mat-header-cell *matHeaderCellDef><span translate>File information</span></th>
<td mat-cell *matCellDef="let file">
<div class="file-info-cell">
<span> <mat-icon [inline]="true">insert_drive_file</mat-icon> {{ file.mediafile.type }} </span>
<span>
<mat-icon [inline]="true">insert_drive_file</mat-icon>
{{ getFiletype(file.mediafile) | translate }}
</span>
<span>
<mat-icon [inline]="true">data_usage</mat-icon>
{{ getReadableSize(file.mediafile.size) }}

View File

@ -158,14 +158,27 @@ export class MediaUploadContentComponent implements OnInit {
}
/**
* Converts a file size in bit into human readable format
* Returns the filetype from the file or a generic "File", if the type could
* not be determinated.
*
* @param bits file size in bits
* @param file The file to get the type from.
*/
public getFiletype(file: File): string {
return file.type || 'File';
}
/**
* Converts a file size in byte into human readable format
*
* @param bytes file size in bytes
* @returns a readable file size representation
*/
public getReadableSize(bits: number): string {
const unitLevel = Math.floor(Math.log(bits) / Math.log(1024));
const bytes = +(bits / Math.pow(1024, unitLevel)).toFixed(2);
public getReadableSize(bytes: number): string {
if (bytes === 0) {
return '0 B';
}
const unitLevel = Math.floor(Math.log(bytes) / Math.log(1024));
bytes = +(bytes / Math.pow(1024, unitLevel)).toFixed(2);
return `${bytes} ${['B', 'kB', 'MB', 'GB', 'TB'][unitLevel]}`;
}

View File

@ -111,7 +111,7 @@
{{ mediafile.filename }}
</div>
<div class="info-text" *ngIf="!mediafile.is_directory">
<span> {{ getDateFromTimestamp(mediafile.timestamp) }} · {{ mediafile.size }} </span>
<span> {{ getDateFromTimestamp(mediafile.timestamp) }} · {{ mediafile.filesize }} </span>
</div>
</div>
</div>

View File

@ -103,14 +103,27 @@ class Mediafile(RESTModelMixin, ListOfSpeakersMixin, models.Model):
"""
`unique_together` is not working with foreign keys with possible `null` values.
So we do need to check this here.
self.original_filename is not yet set, but if is_file is True, the actual
filename is self.mediafile.file
"""
title_or_original_filename = models.Q(title=self.title)
if self.is_file:
title_or_original_filename = title_or_original_filename | models.Q(
original_filename=self.mediafile.name
)
if (
Mediafile.objects.exclude(pk=self.pk)
.filter(title=self.title, parent=self.parent)
Mediafile.objects.exclude(
pk=self.pk
) # self.pk is None on creation, but this does not invalidate the exclude statement.
.filter(title_or_original_filename, parent=self.parent)
.exists()
):
raise ValidationError(
{"detail": "A mediafile with this title already exists in this folder."}
{
"detail": "A mediafile with this title or filename already exists in this folder."
}
)
def __str__(self):

View File

@ -70,7 +70,35 @@ class TestCreation(TestCase):
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertFalse(Mediafile.objects.exists())
def test_mediafile_twice(self):
def test_no_extension(self):
file = SimpleUploadedFile("no_extension", b"some content.")
response = self.client.post(
reverse("mediafile-list"),
{"title": "test_title_vai8oDogohheideedie4", "mediafile": file},
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
mediafile = Mediafile.objects.get()
self.assertEqual(mediafile.title, "test_title_vai8oDogohheideedie4")
def test_mediafile_twice_different_title(self):
file1 = SimpleUploadedFile("file.ext", b"some content.")
file2 = SimpleUploadedFile("file.ext", b"some content.")
response = self.client.post(
reverse("mediafile-list"),
{"title": "test_title_Zeicheipeequie3ohfid", "mediafile": file1},
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
mediafile = Mediafile.objects.get()
self.assertEqual(mediafile.title, "test_title_Zeicheipeequie3ohfid")
response = self.client.post(
reverse("mediafile-list"),
{"title": "test_title_aiChaetohs0quicee9eb", "mediafile": file2},
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(Mediafile.objects.count(), 1)
def test_directory_twice(self):
title = "test_title_kFJq83fjmqo2babfqk3f"
Mediafile.objects.create(is_directory=True, title=title)
response = self.client.post(