diff --git a/3rd-PARTY-LICENSES b/3rd-PARTY-LICENSES index bd29393a..78bfe3bb 100644 --- a/3rd-PARTY-LICENSES +++ b/3rd-PARTY-LICENSES @@ -712,3 +712,31 @@ FileSaver.js is licensed under the MIT license: SOFTWARE. [1]: http://eligrey.com + +croppr.js +========= +https://github.com/jamesssooi/Croppr.js + +croppr.js is licensed under the MIT license: + + MIT License + + Copyright (c) 2017 James Ooi + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. diff --git a/scripts/check_modules.py b/scripts/check_modules.py index 5eb51d1c..301b6163 100644 --- a/scripts/check_modules.py +++ b/scripts/check_modules.py @@ -26,6 +26,7 @@ modules_to_check = { "pycloudflared": "0.2.0", "ruamel.yaml": "0.17.21", "sqlalchemy": "2.0.19", + "python-multipart": "0.0.6", # "xformers": "0.0.16", } modules_to_log = ["torch", "torchvision", "sdkit", "stable-diffusion-sdkit"] diff --git a/ui/easydiffusion/bucket_crud.py b/ui/easydiffusion/bucket_crud.py deleted file mode 100644 index ece14fcc..00000000 --- a/ui/easydiffusion/bucket_crud.py +++ /dev/null @@ -1,36 +0,0 @@ -from sqlalchemy.orm import Session - -from easydiffusion import bucket_models, bucket_schemas - - -def get_bucket(db: Session, bucket_id: int): - return db.query(bucket_models.Bucket).filter(bucket_models.Bucket.id == bucket_id).first() - - -def get_bucket_by_path(db: Session, path: str): - return db.query(bucket_models.Bucket).filter(bucket_models.Bucket.path == path).first() - - -def get_buckets(db: Session, skip: int = 0, limit: int = 100): - return db.query(bucket_models.Bucket).offset(skip).limit(limit).all() - - -def create_bucket(db: Session, bucket: bucket_schemas.BucketCreate): - db_bucket = bucket_models.Bucket(path=bucket.path) - db.add(db_bucket) - db.commit() - db.refresh(db_bucket) - return db_bucket - - -def get_bucketfiles(db: Session, skip: int = 0, limit: int = 100): - return db.query(bucket_models.BucketFile).offset(skip).limit(limit).all() - - -def create_bucketfile(db: Session, bucketfile: bucket_schemas.BucketFileCreate, bucket_id: int): - db_bucketfile = bucket_models.BucketFile(**bucketfile.dict(), bucket_id=bucket_id) - db.add(db_bucketfile) - db.commit() - db.refresh(db_bucketfile) - return db_bucketfile - diff --git a/ui/easydiffusion/bucket_manager.py b/ui/easydiffusion/bucket_manager.py index 0985573f..f587bc7f 100644 --- a/ui/easydiffusion/bucket_manager.py +++ b/ui/easydiffusion/bucket_manager.py @@ -1,16 +1,36 @@ from typing import List -from fastapi import Depends, FastAPI, HTTPException +from fastapi import Depends, FastAPI, HTTPException, Response, File from sqlalchemy.orm import Session -from easydiffusion import bucket_crud, bucket_models, bucket_schemas -from easydiffusion.bucket_database import SessionLocal, engine +from easydiffusion.easydb import crud, models, schemas +from easydiffusion.easydb.database import SessionLocal, engine +from requests.compat import urlparse + +import base64, json + +MIME_TYPES = { + "jpg": "image/jpeg", + "jpeg": "image/jpeg", + "gif": "image/gif", + "png": "image/png", + "webp": "image/webp", + "js": "text/javascript", + "htm": "text/html", + "html": "text/html", + "css": "text/css", + "json": "application/json", + "mjs": "application/json", + "yaml": "application/yaml", + "svg": "image/svg+xml", + "txt": "text/plain", +} def init(): from easydiffusion.server import server_api - bucket_models.BucketBase.metadata.create_all(bind=engine) + models.BucketBase.metadata.create_all(bind=engine) # Dependency @@ -21,37 +41,63 @@ def init(): finally: db.close() + @server_api.get("/bucket/{obj_path:path}") + def bucket_get_object(obj_path: str, db: Session = Depends(get_db)): + filename = get_filename_from_url(obj_path) + path = get_path_from_url(obj_path) - @server_api.post("/buckets/", response_model=bucket_schemas.Bucket) - def create_bucket(bucket: bucket_schemas.BucketCreate, db: Session = Depends(get_db)): - db_bucket = bucket_crud.get_bucket_by_path(db, path=bucket.path) - if db_bucket: - raise HTTPException(status_code=400, detail="Bucket already exists") - return bucket_crud.create_bucket(db=db, bucket=bucket) + if filename==None: + bucket = crud.get_bucket_by_path(db, path=path) + if bucket == None: + raise HTTPException(status_code=404, detail="Bucket not found") + bucketfiles = db.query(models.BucketFile).with_entities(models.BucketFile.filename).filter(models.BucketFile.bucket_id == bucket.id).all() + bucketfiles = [ x.filename for x in bucketfiles ] + return bucketfiles - @server_api.get("/buckets/", response_model=List[bucket_schemas.Bucket]) - def read_bucket(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): - buckets = bucket_crud.get_buckets(db, skip=skip, limit=limit) - return buckets + else: + bucket_id = crud.get_bucket_by_path(db, path).id + bucketfile = db.query(models.BucketFile).filter(models.BucketFile.bucket_id == bucket_id, models.BucketFile.filename == filename).first() + + suffix = get_suffix_from_filename(filename) + + return Response(content=bucketfile.data, media_type=MIME_TYPES.get(suffix, "application/octet-stream")) + + @server_api.post("/bucket/{obj_path:path}") + def bucket_post_object(obj_path: str, file: bytes = File(), db: Session = Depends(get_db)): + filename = get_filename_from_url(obj_path) + path = get_path_from_url(obj_path) + bucket = crud.get_bucket_by_path(db, path) + + if bucket == None: + bucket_id = crud.create_bucket(db=db, bucket=schemas.BucketCreate(path=path)) + else: + bucket_id = bucket.id + + bucketfile = schemas.BucketFileCreate(filename=filename, data=file) + result = crud.create_bucketfile(db=db, bucketfile=bucketfile, bucket_id=bucket_id) + result.data = base64.encodestring(result.data) + return result - @server_api.get("/buckets/{bucket_id}", response_model=bucket_schemas.Bucket) - def read_bucket(bucket_id: int, db: Session = Depends(get_db)): - db_bucket = bucket_crud.get_bucket(db, bucket_id=bucket_id) - if db_bucket is None: - raise HTTPException(status_code=404, detail="Bucket not found") - return db_bucket - - - @server_api.post("/buckets/{bucket_id}/items/", response_model=bucket_schemas.BucketFile) + @server_api.post("/buckets/{bucket_id}/items/", response_model=schemas.BucketFile) def create_bucketfile_in_bucket( - bucket_id: int, bucketfile: bucket_schemas.BucketFileCreate, db: Session = Depends(get_db) + bucket_id: int, bucketfile: schemas.BucketFileCreate, db: Session = Depends(get_db) ): - return bucket_crud.create_bucketfile(db=db, bucketfile=bucketfile, bucket_id=bucket_id) + bucketfile.data = base64.decodestring(bucketfile.data) + result = crud.create_bucketfile(db=db, bucketfile=bucketfile, bucket_id=bucket_id) + result.data = base64.encodestring(result.data) + return result - @server_api.get("/bucketfiles/", response_model=List[bucket_schemas.BucketFile]) - def read_bucketfiles(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): - bucketfiles = bucket_crud.get_bucketfiles(db, skip=skip, limit=limit) - return bucketfiles +def get_filename_from_url(url): + path = urlparse(url).path + name = path[path.rfind('/')+1:] + return name or None +def get_path_from_url(url): + path = urlparse(url).path + path = path[0:path.rfind('/')] + return path or None + +def get_suffix_from_filename(filename): + return filename[filename.rfind('.')+1:] diff --git a/ui/easydiffusion/easydb/crud.py b/ui/easydiffusion/easydb/crud.py new file mode 100644 index 00000000..7550a52a --- /dev/null +++ b/ui/easydiffusion/easydb/crud.py @@ -0,0 +1,25 @@ +from sqlalchemy.orm import Session + +from easydiffusion.easydb import models, schemas + + +def get_bucket_by_path(db: Session, path: str): + return db.query(models.Bucket).filter(models.Bucket.path == path).first() + + +def create_bucket(db: Session, bucket: schemas.BucketCreate): + db_bucket = models.Bucket(path=bucket.path) + db.add(db_bucket) + db.commit() + db.refresh(db_bucket) + return db_bucket + + +def create_bucketfile(db: Session, bucketfile: schemas.BucketFileCreate, bucket_id: int): + db_bucketfile = models.BucketFile(**bucketfile.dict(), bucket_id=bucket_id) + db.merge(db_bucketfile) + db.commit() + from pprint import pprint + db_bucketfile = db.query(models.BucketFile).filter(models.BucketFile.bucket_id==bucket_id, models.BucketFile.filename==bucketfile.filename).first() + return db_bucketfile + diff --git a/ui/easydiffusion/bucket_database.py b/ui/easydiffusion/easydb/database.py similarity index 100% rename from ui/easydiffusion/bucket_database.py rename to ui/easydiffusion/easydb/database.py diff --git a/ui/easydiffusion/bucket_models.py b/ui/easydiffusion/easydb/models.py similarity index 71% rename from ui/easydiffusion/bucket_models.py rename to ui/easydiffusion/easydb/models.py index 3ede1a36..04834951 100644 --- a/ui/easydiffusion/bucket_models.py +++ b/ui/easydiffusion/easydb/models.py @@ -1,7 +1,7 @@ from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, BLOB from sqlalchemy.orm import relationship -from easydiffusion.bucket_database import BucketBase +from easydiffusion.easydb.database import BucketBase class Bucket(BucketBase): @@ -16,10 +16,10 @@ class Bucket(BucketBase): class BucketFile(BucketBase): __tablename__ = "bucketfile" - id = Column(Integer, primary_key=True, index=True) - filename = Column(String, index=True) + filename = Column(String, index=True, primary_key=True) + bucket_id = Column(Integer, ForeignKey("bucket.id"), primary_key=True) + data = Column(BLOB, index=False) - bucket_id = Column(Integer, ForeignKey("bucket.id")) bucket = relationship("Bucket", back_populates="bucketfiles") diff --git a/ui/easydiffusion/bucket_schemas.py b/ui/easydiffusion/easydb/schemas.py similarity index 97% rename from ui/easydiffusion/bucket_schemas.py rename to ui/easydiffusion/easydb/schemas.py index 9f7e2377..68bc04e2 100644 --- a/ui/easydiffusion/bucket_schemas.py +++ b/ui/easydiffusion/easydb/schemas.py @@ -13,7 +13,6 @@ class BucketFileCreate(BucketFileBase): class BucketFile(BucketFileBase): - id: int bucket_id: int class Config: diff --git a/ui/index.html b/ui/index.html index 0fcb807d..de4273aa 100644 --- a/ui/index.html +++ b/ui/index.html @@ -18,12 +18,14 @@ + +
@@ -630,6 +632,35 @@
+ +
+
+

Use as thumbnail

+ Use a pictures as thumbnail for embeddings, LORAs, etc. +
+
+ +
+
+
+
+
+ +
+
+

Use the thumbnail for …

+ +
+
+ + +
+
+
+
+