|
| 1 | +# coding=utf-8 |
| 2 | +""" |
| 3 | + @project: maxkb |
| 4 | + @Author:虎 |
| 5 | + @file: image_serializers.py |
| 6 | + @date:2024/4/22 16:36 |
| 7 | + @desc: |
| 8 | +""" |
| 9 | +import uuid |
| 10 | + |
| 11 | +from django.db.models import QuerySet |
| 12 | +from django.http import HttpResponse |
| 13 | +from rest_framework import serializers |
| 14 | + |
| 15 | +from common.exception.app_exception import NotFound404 |
| 16 | +from common.field.common import UploadedFileField |
| 17 | +from common.util.field_message import ErrMessage |
| 18 | +from dataset.models import File |
| 19 | + |
| 20 | +mime_types = {"html": "text/html", "htm": "text/html", "shtml": "text/html", "css": "text/css", "xml": "text/xml", |
| 21 | + "gif": "image/gif", "jpeg": "image/jpeg", "jpg": "image/jpeg", "js": "application/javascript", |
| 22 | + "atom": "application/atom+xml", "rss": "application/rss+xml", "mml": "text/mathml", "txt": "text/plain", |
| 23 | + "jad": "text/vnd.sun.j2me.app-descriptor", "wml": "text/vnd.wap.wml", "htc": "text/x-component", |
| 24 | + "avif": "image/avif", "png": "image/png", "svg": "image/svg+xml", "svgz": "image/svg+xml", |
| 25 | + "tif": "image/tiff", "tiff": "image/tiff", "wbmp": "image/vnd.wap.wbmp", "webp": "image/webp", |
| 26 | + "ico": "image/x-icon", "jng": "image/x-jng", "bmp": "image/x-ms-bmp", "woff": "font/woff", |
| 27 | + "woff2": "font/woff2", "jar": "application/java-archive", "war": "application/java-archive", |
| 28 | + "ear": "application/java-archive", "json": "application/json", "hqx": "application/mac-binhex40", |
| 29 | + "doc": "application/msword", "pdf": "application/pdf", "ps": "application/postscript", |
| 30 | + "eps": "application/postscript", "ai": "application/postscript", "rtf": "application/rtf", |
| 31 | + "m3u8": "application/vnd.apple.mpegurl", "kml": "application/vnd.google-earth.kml+xml", |
| 32 | + "kmz": "application/vnd.google-earth.kmz", "xls": "application/vnd.ms-excel", |
| 33 | + "eot": "application/vnd.ms-fontobject", "ppt": "application/vnd.ms-powerpoint", |
| 34 | + "odg": "application/vnd.oasis.opendocument.graphics", |
| 35 | + "odp": "application/vnd.oasis.opendocument.presentation", |
| 36 | + "ods": "application/vnd.oasis.opendocument.spreadsheet", "odt": "application/vnd.oasis.opendocument.text", |
| 37 | + "wmlc": "application/vnd.wap.wmlc", "wasm": "application/wasm", "7z": "application/x-7z-compressed", |
| 38 | + "cco": "application/x-cocoa", "jardiff": "application/x-java-archive-diff", |
| 39 | + "jnlp": "application/x-java-jnlp-file", "run": "application/x-makeself", "pl": "application/x-perl", |
| 40 | + "pm": "application/x-perl", "prc": "application/x-pilot", "pdb": "application/x-pilot", |
| 41 | + "rar": "application/x-rar-compressed", "rpm": "application/x-redhat-package-manager", |
| 42 | + "sea": "application/x-sea", "swf": "application/x-shockwave-flash", "sit": "application/x-stuffit", |
| 43 | + "tcl": "application/x-tcl", "tk": "application/x-tcl", "der": "application/x-x509-ca-cert", |
| 44 | + "pem": "application/x-x509-ca-cert", "crt": "application/x-x509-ca-cert", |
| 45 | + "xpi": "application/x-xpinstall", "xhtml": "application/xhtml+xml", "xspf": "application/xspf+xml", |
| 46 | + "zip": "application/zip", "bin": "application/octet-stream", "exe": "application/octet-stream", |
| 47 | + "dll": "application/octet-stream", "deb": "application/octet-stream", "dmg": "application/octet-stream", |
| 48 | + "iso": "application/octet-stream", "img": "application/octet-stream", "msi": "application/octet-stream", |
| 49 | + "msp": "application/octet-stream", "msm": "application/octet-stream", "mid": "audio/midi", |
| 50 | + "midi": "audio/midi", "kar": "audio/midi", "mp3": "audio/mpeg", "ogg": "audio/ogg", "m4a": "audio/x-m4a", |
| 51 | + "ra": "audio/x-realaudio", "3gpp": "video/3gpp", "3gp": "video/3gpp", "ts": "video/mp2t", |
| 52 | + "mp4": "video/mp4", "mpeg": "video/mpeg", "mpg": "video/mpeg", "mov": "video/quicktime", |
| 53 | + "webm": "video/webm", "flv": "video/x-flv", "m4v": "video/x-m4v", "mng": "video/x-mng", |
| 54 | + "asx": "video/x-ms-asf", "asf": "video/x-ms-asf", "wmv": "video/x-ms-wmv", "avi": "video/x-msvideo"} |
| 55 | + |
| 56 | + |
| 57 | +class FileSerializer(serializers.Serializer): |
| 58 | + file = UploadedFileField(required=True, error_messages=ErrMessage.image("文件")) |
| 59 | + |
| 60 | + def upload(self, with_valid=True): |
| 61 | + if with_valid: |
| 62 | + self.is_valid(raise_exception=True) |
| 63 | + file_id = uuid.uuid1() |
| 64 | + file = File(id=file_id, file_name=self.data.get('file').name) |
| 65 | + file.save(self.data.get('file').read()) |
| 66 | + return f'/api/file/{file_id}' |
| 67 | + |
| 68 | + class Operate(serializers.Serializer): |
| 69 | + id = serializers.UUIDField(required=True) |
| 70 | + |
| 71 | + def get(self, with_valid=True): |
| 72 | + if with_valid: |
| 73 | + self.is_valid(raise_exception=True) |
| 74 | + file_id = self.data.get('id') |
| 75 | + file = QuerySet(File).filter(id=file_id).first() |
| 76 | + if file is None: |
| 77 | + raise NotFound404(404, "不存在的文件") |
| 78 | + return HttpResponse(file.get_byte(), status=200, |
| 79 | + headers={'Content-Type': mime_types.get(file.file_name.split(".")[-1], 'text/plain')}) |
0 commit comments