fix embedded file upload
This commit is contained in:
parent
aa0bb9fd0d
commit
27589a09bd
4 changed files with 76 additions and 19 deletions
|
@ -14,11 +14,19 @@ class FileManager(models.Manager):
|
|||
if 'data' in kwargs and type(kwargs['data']) == str:
|
||||
import base64
|
||||
from hashlib import sha256
|
||||
content = base64.b64decode(kwargs['data'], validate=True)
|
||||
raw = kwargs['data']
|
||||
if not raw.startswith('data:'):
|
||||
raise ValueError('data must be a base64 encoded string or file and hash must be provided')
|
||||
raw = raw.split(';base64,')
|
||||
if len(raw) != 2:
|
||||
raise ValueError('data must be a base64 encoded string or file and hash must be provided')
|
||||
mime_type = raw[0].split(':')[1]
|
||||
content = base64.b64decode(raw[1], validate=True)
|
||||
kwargs.pop('data')
|
||||
content_hash = sha256(content).hexdigest()
|
||||
kwargs['file'] = ContentFile(content, content_hash)
|
||||
kwargs['hash'] = content_hash
|
||||
kwargs['mime_type'] = mime_type
|
||||
else:
|
||||
raise ValueError('data must be a base64 encoded string or file and hash must be provided')
|
||||
try:
|
||||
|
@ -30,11 +38,19 @@ class FileManager(models.Manager):
|
|||
if 'data' in kwargs and type(kwargs['data']) == str:
|
||||
import base64
|
||||
from hashlib import sha256
|
||||
content = base64.b64decode(kwargs['data'], validate=True)
|
||||
raw = kwargs['data']
|
||||
if not raw.startswith('data:'):
|
||||
raise ValueError('data must be a base64 encoded string or file and hash must be provided')
|
||||
raw = raw.split(';base64,')
|
||||
if len(raw) != 2:
|
||||
raise ValueError('data must be a base64 encoded string or file and hash must be provided')
|
||||
mime_type = raw[0].split(':')[1]
|
||||
content = base64.b64decode(raw[1], validate=True)
|
||||
kwargs.pop('data')
|
||||
content_hash = sha256(content).hexdigest()
|
||||
kwargs['file'] = ContentFile(content, content_hash)
|
||||
kwargs['hash'] = content_hash
|
||||
kwargs['mime_type'] = mime_type
|
||||
elif 'file' in kwargs and 'hash' in kwargs and type(kwargs['file']) == ContentFile:
|
||||
pass
|
||||
else:
|
||||
|
|
|
@ -15,7 +15,7 @@ class FileTestCase(TestCase):
|
|||
|
||||
def test_list_files(self):
|
||||
import base64
|
||||
item = File.objects.create(data=base64.b64encode(b"foo").decode('utf-8'))
|
||||
item = File.objects.create(data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8'))
|
||||
response = client.get('/api/1/files')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()[0]['hash'], item.hash)
|
||||
|
@ -23,7 +23,7 @@ class FileTestCase(TestCase):
|
|||
|
||||
def test_one_file(self):
|
||||
import base64
|
||||
item = File.objects.create(data=base64.b64encode(b"foo").decode('utf-8'))
|
||||
item = File.objects.create(data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8'))
|
||||
response = client.get(f'/api/1/file/{item.hash}')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()['hash'], item.hash)
|
||||
|
@ -33,15 +33,17 @@ class FileTestCase(TestCase):
|
|||
import base64
|
||||
Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='2')
|
||||
response = client.post('/api/1/file', {'data': base64.b64encode(b"foo").decode('utf-8')}, content_type='application/json')
|
||||
response = client.post('/api/1/file',
|
||||
{'data': "data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8')},
|
||||
content_type='application/json')
|
||||
self.assertEqual(response.status_code, 201)
|
||||
self.assertEqual(len(response.json()['hash']), 64)
|
||||
|
||||
def test_delete_file(self):
|
||||
import base64
|
||||
item = Item.objects.create(container=self.box, event=self.event, description='1')
|
||||
File.objects.create(item=item, data=base64.b64encode(b"foo").decode('utf-8'))
|
||||
file = File.objects.create(item=item, data=base64.b64encode(b"bar").decode('utf-8'))
|
||||
File.objects.create(item=item, data="data:text/plain;base64," + base64.b64encode(b"foo").decode('utf-8'))
|
||||
file = File.objects.create(item=item, data="data:text/plain;base64," + base64.b64encode(b"bar").decode('utf-8'))
|
||||
self.assertEqual(len(File.objects.all()), 2)
|
||||
response = client.delete(f'/api/1/file/{file.hash}')
|
||||
self.assertEqual(response.status_code, 204)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue