Created
April 6, 2022 01:28
-
-
Save doutv/63bb3691d005aa5060dc84498f0b77e8 to your computer and use it in GitHub Desktop.
Django custom backend storage for sm.ms image hosting service 图床
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
from django.core.files.storage import Storage | |
from django.conf import settings | |
from django.utils.deconstruct import deconstructible | |
import requests | |
from django.core.files import File | |
import logging | |
logger = logging.getLogger(__name__) | |
@deconstructible | |
class SmmsStorage(Storage): | |
# https://sm.ms/ | |
def __init__(self): | |
''' | |
in settings.py | |
CUSTOM_STORAGE_OPTIONS = { | |
"base_url": "https://sm.ms/api/v2", | |
"token": os.environ.get("SMMS_TOKEN", "") | |
} | |
''' | |
self.option = settings.CUSTOM_STORAGE_OPTIONS | |
def _open(self, name, mode='rb'): | |
pass | |
def _save(self, name, content): | |
files = {'smfile': (name, File(content, name))} | |
headers = {'Authorization': self.option["token"]} | |
upload_url = self.option["base_url"] + '/upload' | |
resp = requests.post(upload_url, files=files, headers=headers) | |
if resp.status_code != requests.codes.ok: | |
logger.error( | |
f"Image Upload Failed: {resp.status_code}-{resp.content}") | |
return | |
if (resp.json()["success"] == True): | |
filename = resp.json()['data']['url'] # url as name | |
elif (resp.json()["code"] == "image_repeated"): | |
filename = resp.json()['images'] | |
else: | |
logger.error(f"Image Upload Failed: {resp.json()}") | |
return filename | |
def url(self, name): | |
return name | |
def delete(self, name): | |
pass | |
def exists(self, name): | |
return False | |
def listdir(self, path): | |
pass | |
def size(self, name): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Usage:
in
models.py
:photo = models.ImageField(storage=SmmsStorage())