baichongyang 3 years ago
commit
ec37782a10
5 changed files with 101 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 15 0
      database.py
  3. 64 0
      main.py
  4. 10 0
      model.py
  5. 11 0
      schema.py

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+/data

+ 15 - 0
database.py

@@ -0,0 +1,15 @@
+from sqlalchemy import create_engine
+from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy.orm import sessionmaker
+from sqlalchemy.engine.url import URL
+# DATABASE_URL = "mysql+mysqlconnector://wx2016_longvideo:wx2016_longvideoP@assword1234@rm-bp1k5853td1r25g3n690.mysql.rds.aliyuncs.com:3306/webeditter"
+engine = create_engine(URL(
+    'mysql',
+    username='wx2016_longvideo',
+    password='wx2016_longvideoP@assword1234',
+    host='rm-bp1k5853td1r25g3n690.mysql.rds.aliyuncs.com',
+    database='webeditter'
+))
+SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
+Base = declarative_base()
+

+ 64 - 0
main.py

@@ -0,0 +1,64 @@
+from typing import Optional
+from fastapi import FastAPI, UploadFile, Depends, Request
+from fastapi.staticfiles import StaticFiles
+from sqlalchemy.orm import Session
+import hashlib
+import time
+import schema
+from database import SessionLocal, engine
+import model
+
+base_host = '/data/'
+model.Base.metadata.create_all(bind=engine)
+app = FastAPI()
+app.mount("/data", StaticFiles(directory="data"), name="data")
+
+
+
+def get_database_session():
+    try:
+        db = SessionLocal()
+        yield db
+    finally:
+        db.close()
+
+# @app.get("/")
+# def read_root():
+#     return {"Hello": "World"}
+#
+#
+# @app.get("/items/{item_id}")
+# def read_item(item_id: int, q: Optional[str] = None):
+#     return {"item_id": item_id, "q": q}
+
+
+@app.post("/uploadfile/")
+def create_upload_file(file: Optional[UploadFile] = None, db: Session = Depends(get_database_session)):
+    if not file:
+        return {"code": "0", "msg": "no file"}
+    else:
+        file_location = f"./data/{file.filename}"
+        with open(file_location, "wb+") as file_object:
+            data = file.file.read()
+            md5 = hashlib.md5(data).hexdigest()
+            try:
+                audio = model.CryptoAudio(md5=md5, name=file.filename, tm=time.time())
+                db.add(audio)
+                db.commit()
+                db.refresh(audio)
+            except Exception as e:
+                return {"code": "-1", "msg": str(e)}
+            file_object.write(file.file.read())
+        return {"code": "0", "msg": "ok", 'data': {'md5': md5}}
+
+
+@app.get("/get_audio_file/{md5}")
+def get_audio_file(md5: str, db: Session = Depends(get_database_session)):
+    try:
+        item = db.query(model.CryptoAudio).filter(md5 == md5).first()
+    except Exception as e:
+        return {"code": "-1", "msg": str(e)}
+    if item:
+        return {"code": "0", "msg": "ok", 'data': {'url': f"{base_host}{item.name}"}}
+    else:
+        return {"code": "-1", "msg": "no exist"}

+ 10 - 0
model.py

@@ -0,0 +1,10 @@
+from sqlalchemy.schema import Column
+from sqlalchemy.types import String, Integer, Float
+from database import Base
+
+class CryptoAudio(Base):
+    __tablename__ = 'cryptoaudio'
+    id = Column(Integer, primary_key=True, index=True)
+    md5 = Column(String(50), unique=True)
+    name = Column(String(50), unique=False)
+    tm = Column(Float)

+ 11 - 0
schema.py

@@ -0,0 +1,11 @@
+from datetime import date
+from pydantic import BaseModel
+
+class CryptoAudio(BaseModel):
+    id = int
+    md5 = str
+    name = str
+    tm = float
+
+    class Config:
+        orm_mode = True