-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Broke out add screenshot and swapped out cli commands
- Loading branch information
1 parent
8d03788
commit 7e9219f
Showing
14 changed files
with
444 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
from openra.classes.exceptions import ExceptionBase | ||
|
||
|
||
class ScreenshotResource: | ||
|
||
type: str | ||
id: int | ||
|
||
def __init__(self, type: str, id: int): | ||
if type not in ['maps']: | ||
raise ExceptionScreenshotResourceTypeInvalid(type, id) | ||
|
||
self.type = type | ||
self.id = id | ||
|
||
|
||
class ExceptionScreenshotResourceTypeInvalid(ExceptionBase): | ||
def __init__(self, type: str, id: int): | ||
super().__init__() | ||
self.message = "Invalid resource type for screenshot resource" | ||
self.detail.append('Type : ' + type) | ||
self.detail.append('Id: ' + str(id)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import io | ||
from dependency_injector.wiring import Provide, inject | ||
from django.conf import os | ||
from django.contrib.auth.models import User | ||
from django.core.files.uploadedfile import UploadedFile | ||
from django.utils import timezone | ||
from fs.base import FS, copy | ||
from openra.classes.exceptions import ExceptionBase | ||
from openra.classes.file_location import FileLocation | ||
from openra.classes.screenshot_resource import ScreenshotResource | ||
from openra.models import Screenshots | ||
from PIL import Image | ||
|
||
from openra.services.uploaded_file_importer import UploadedFileImporter | ||
|
||
|
||
class ScreenshotRepository: | ||
|
||
_data_fs: FS | ||
_uploaded_file_importer: UploadedFileImporter | ||
|
||
@inject | ||
def __init__( | ||
self, | ||
data_fs: FS = Provide['data_fs'], | ||
uploaded_file_importer: UploadedFileImporter = Provide['uploaded_file_importer'] | ||
): | ||
self._data_fs = data_fs | ||
self._uploaded_file_importer = uploaded_file_importer | ||
|
||
def create_from_uploaded_file(self, uploaded_file: UploadedFile, user: User, resource: ScreenshotResource, map_preview: bool): | ||
|
||
if uploaded_file.content_type not in ['image/jpeg', 'image/png', 'image/gif']: | ||
raise ExceptionInvalidMimeType(uploaded_file.name, uploaded_file.content_type) | ||
|
||
extension = uploaded_file.content_type.split('/')[1] | ||
|
||
uploaded = self._uploaded_file_importer.import_file( | ||
uploaded_file, | ||
uploaded_file.name | ||
) | ||
|
||
image = Image.open( | ||
uploaded.get_file_clone() | ||
) | ||
|
||
image.thumbnail(( | ||
250, | ||
250 | ||
)) | ||
|
||
thumbnail = io.BytesIO() | ||
|
||
image.save(thumbnail, extension) | ||
|
||
thumbnail.seek(0) | ||
|
||
model = Screenshots( | ||
user=user, | ||
ex_id=resource.id, | ||
ex_name=resource.type, | ||
posted=timezone.now(), | ||
map_preview=map_preview, | ||
) | ||
|
||
model.save() | ||
|
||
directory = os.path.join('screenshots', str(model.id)) | ||
|
||
uploaded.copy_to_file_location( | ||
FileLocation( | ||
self._data_fs, | ||
directory, | ||
str(resource.id) + '.' + extension | ||
) | ||
) | ||
|
||
preview_path = os.path.join('screenshots', str(model.id), str(resource.id) + '-mini.' + extension) | ||
|
||
self._data_fs.writefile( | ||
preview_path, | ||
thumbnail | ||
) | ||
|
||
return model | ||
|
||
|
||
class ExceptionInvalidMimeType(ExceptionBase): | ||
def __init__(self, filename: str, mimetype): | ||
super().__init__() | ||
self.message = "Mimetype invalid for a screenshot" | ||
self.detail.append('Filename : ' + filename) | ||
self.detail.append('Mimetype: ' + str(mimetype)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from django.core.files.uploadedfile import File | ||
from fs.tempfs import TempFS | ||
|
||
from openra.classes.exceptions import ExceptionBase | ||
from openra.classes.file_location import FileLocation | ||
|
||
|
||
class UploadedFileImporter: | ||
|
||
def import_file(self, request_file: File, filename: str): | ||
|
||
try: | ||
temp_fs = self._create_temp_fs() | ||
|
||
for chunk in request_file.chunks(): | ||
temp_fs.appendbytes( | ||
filename, | ||
chunk | ||
) | ||
|
||
return FileLocation( | ||
temp_fs, | ||
'', | ||
filename | ||
) | ||
except Exception as exception: | ||
raise ExceptionUploadedFileImporter(exception, filename) | ||
|
||
def _create_temp_fs(self): | ||
return TempFS() | ||
|
||
|
||
class ExceptionUploadedFileImporter(ExceptionBase): | ||
def __init__(self, exception, filename: str): | ||
super().__init__() | ||
self.message = "Uploaded file importer caught an exception while attempting to upload this file" | ||
self.detail.append('filename: ' + filename) | ||
self.detail.append('message: ' + str(exception)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.