mirror of
https://github.com/easydiffusion/easydiffusion.git
synced 2025-06-21 18:31:28 +02:00
Move easydb files to its own folders
This commit is contained in:
parent
97035a54ed
commit
5f736368c8
@ -1,37 +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.merge(db_bucketfile)
|
|
||||||
db.commit()
|
|
||||||
from pprint import pprint
|
|
||||||
db_bucketfile = db.query(bucket_models.BucketFile).filter(bucket_models.BucketFile.bucket_id==bucket_id, bucket_models.BucketFile.filename==bucketfile.filename).first()
|
|
||||||
return db_bucketfile
|
|
||||||
|
|
@ -3,8 +3,8 @@ from typing import List
|
|||||||
from fastapi import Depends, FastAPI, HTTPException, Response, File
|
from fastapi import Depends, FastAPI, HTTPException, Response, File
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from easydiffusion import bucket_crud, bucket_models, bucket_schemas
|
from easydiffusion.easydb import crud, models, schemas
|
||||||
from easydiffusion.bucket_database import SessionLocal, engine
|
from easydiffusion.easydb.database import SessionLocal, engine
|
||||||
|
|
||||||
from requests.compat import urlparse
|
from requests.compat import urlparse
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ MIME_TYPES = {
|
|||||||
def init():
|
def init():
|
||||||
from easydiffusion.server import server_api
|
from easydiffusion.server import server_api
|
||||||
|
|
||||||
bucket_models.BucketBase.metadata.create_all(bind=engine)
|
models.BucketBase.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
|
|
||||||
# Dependency
|
# Dependency
|
||||||
@ -47,16 +47,16 @@ def init():
|
|||||||
path = get_path_from_url(obj_path)
|
path = get_path_from_url(obj_path)
|
||||||
|
|
||||||
if filename==None:
|
if filename==None:
|
||||||
bucket = bucket_crud.get_bucket_by_path(db, path=path)
|
bucket = crud.get_bucket_by_path(db, path=path)
|
||||||
if bucket == None:
|
if bucket == None:
|
||||||
raise HTTPException(status_code=404, detail="Bucket not found")
|
raise HTTPException(status_code=404, detail="Bucket not found")
|
||||||
bucketfiles = db.query(bucket_models.BucketFile).with_entities(bucket_models.BucketFile.filename).filter(bucket_models.BucketFile.bucket_id == bucket.id).all()
|
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 ]
|
bucketfiles = [ x.filename for x in bucketfiles ]
|
||||||
return bucketfiles
|
return bucketfiles
|
||||||
|
|
||||||
else:
|
else:
|
||||||
bucket_id = bucket_crud.get_bucket_by_path(db, path).id
|
bucket_id = crud.get_bucket_by_path(db, path).id
|
||||||
bucketfile = db.query(bucket_models.BucketFile).filter(bucket_models.BucketFile.bucket_id == bucket_id, bucket_models.BucketFile.filename == filename).first()
|
bucketfile = db.query(models.BucketFile).filter(models.BucketFile.bucket_id == bucket_id, models.BucketFile.filename == filename).first()
|
||||||
|
|
||||||
suffix = get_suffix_from_filename(filename)
|
suffix = get_suffix_from_filename(filename)
|
||||||
|
|
||||||
@ -66,55 +66,29 @@ def init():
|
|||||||
def bucket_post_object(obj_path: str, file: bytes = File(), db: Session = Depends(get_db)):
|
def bucket_post_object(obj_path: str, file: bytes = File(), db: Session = Depends(get_db)):
|
||||||
filename = get_filename_from_url(obj_path)
|
filename = get_filename_from_url(obj_path)
|
||||||
path = get_path_from_url(obj_path)
|
path = get_path_from_url(obj_path)
|
||||||
bucket = bucket_crud.get_bucket_by_path(db, path)
|
bucket = crud.get_bucket_by_path(db, path)
|
||||||
|
|
||||||
if bucket == None:
|
if bucket == None:
|
||||||
bucket_id = bucket_crud.create_bucket(db=db, bucket=bucket_schemas.BucketCreate(path=path))
|
bucket_id = crud.create_bucket(db=db, bucket=schemas.BucketCreate(path=path))
|
||||||
else:
|
else:
|
||||||
bucket_id = bucket.id
|
bucket_id = bucket.id
|
||||||
|
|
||||||
bucketfile = bucket_schemas.BucketFileCreate(filename=filename, data=file)
|
bucketfile = schemas.BucketFileCreate(filename=filename, data=file)
|
||||||
result = bucket_crud.create_bucketfile(db=db, bucketfile=bucketfile, bucket_id=bucket_id)
|
result = crud.create_bucketfile(db=db, bucketfile=bucketfile, bucket_id=bucket_id)
|
||||||
result.data = base64.encodestring(result.data)
|
result.data = base64.encodestring(result.data)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@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)
|
|
||||||
|
|
||||||
@server_api.get("/buckets/", response_model=List[bucket_schemas.Bucket])
|
@server_api.post("/buckets/{bucket_id}/items/", response_model=schemas.BucketFile)
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
@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)
|
|
||||||
def create_bucketfile_in_bucket(
|
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)
|
||||||
):
|
):
|
||||||
bucketfile.data = base64.decodestring(bucketfile.data)
|
bucketfile.data = base64.decodestring(bucketfile.data)
|
||||||
result = bucket_crud.create_bucketfile(db=db, bucketfile=bucketfile, bucket_id=bucket_id)
|
result = crud.create_bucketfile(db=db, bucketfile=bucketfile, bucket_id=bucket_id)
|
||||||
result.data = base64.encodestring(result.data)
|
result.data = base64.encodestring(result.data)
|
||||||
return result
|
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):
|
def get_filename_from_url(url):
|
||||||
path = urlparse(url).path
|
path = urlparse(url).path
|
||||||
name = path[path.rfind('/')+1:]
|
name = path[path.rfind('/')+1:]
|
||||||
|
25
ui/easydiffusion/easydb/crud.py
Normal file
25
ui/easydiffusion/easydb/crud.py
Normal file
@ -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
|
||||||
|
|
@ -1,7 +1,7 @@
|
|||||||
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, BLOB
|
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, BLOB
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship
|
||||||
|
|
||||||
from easydiffusion.bucket_database import BucketBase
|
from easydiffusion.easydb.database import BucketBase
|
||||||
|
|
||||||
|
|
||||||
class Bucket(BucketBase):
|
class Bucket(BucketBase):
|
Loading…
x
Reference in New Issue
Block a user