filestorage
This commit is contained in:
0
FileStorage/__init__.py
Normal file
0
FileStorage/__init__.py
Normal file
21
FileStorage/root.py
Normal file
21
FileStorage/root.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import os
|
||||
from os import mkdir
|
||||
from os.path import exists
|
||||
|
||||
from aiohttp import web
|
||||
from FileStorage.routes import setup_routes
|
||||
|
||||
|
||||
def runserver():
|
||||
app = web.Application()
|
||||
setup_routes(app)
|
||||
if not exists("data"):
|
||||
mkdir("data")
|
||||
if not exists("data/meta.txt"):
|
||||
with open("data/meta.txt", "w") as fs:
|
||||
fs.write("0")
|
||||
web.run_app(app, host=os.getenv("FS_HOST", "0.0.0.0"), port=5555)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
runserver()
|
9
FileStorage/routes.py
Normal file
9
FileStorage/routes.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from aiohttp import web
|
||||
|
||||
from FileStorage.views import get_file, upload_file, delete_file
|
||||
|
||||
|
||||
def setup_routes(app: web.Application):
|
||||
app.router.add_get("/get_file", get_file)
|
||||
app.router.add_post("/upload_file", upload_file)
|
||||
app.router.add_post("/delete_file", delete_file)
|
26
FileStorage/sync.py
Normal file
26
FileStorage/sync.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import threading
|
||||
|
||||
import aiofiles
|
||||
|
||||
|
||||
def synchronized_method(method):
|
||||
outer_lock = threading.Lock()
|
||||
lock_name = "__" + method.__name__ + "_lock" + "__"
|
||||
|
||||
def sync_method(self, *args, **kws):
|
||||
with outer_lock:
|
||||
if not hasattr(self, lock_name):
|
||||
setattr(self, lock_name, threading.Lock())
|
||||
lock = getattr(self, lock_name)
|
||||
with lock:
|
||||
return method(self, *args, **kws)
|
||||
return sync_method
|
||||
|
||||
|
||||
@synchronized_method
|
||||
async def write_meta(request):
|
||||
async with aiofiles.open("data/meta.txt", "r") as fs:
|
||||
num = int(await fs.read()) + 1
|
||||
async with aiofiles.open("data/meta.txt", "w") as fs:
|
||||
await fs.write(str(num))
|
||||
return num
|
3
FileStorage/views/__init__.py
Normal file
3
FileStorage/views/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .get_file import get_file
|
||||
from .upload_file import upload_file
|
||||
from .delete_file import delete_file
|
8
FileStorage/views/delete_file.py
Normal file
8
FileStorage/views/delete_file.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from os import remove
|
||||
|
||||
from aiohttp import web
|
||||
|
||||
|
||||
async def delete_file(request):
|
||||
remove("data/" + request.rel_url.query['id'])
|
||||
return web.json_response({"success": True})
|
10
FileStorage/views/get_file.py
Normal file
10
FileStorage/views/get_file.py
Normal file
@@ -0,0 +1,10 @@
|
||||
import aiofiles
|
||||
from aiohttp import web
|
||||
|
||||
|
||||
async def get_file(request):
|
||||
response = web.StreamResponse()
|
||||
await response.prepare(request)
|
||||
async with aiofiles.open("data/" + request.rel_url.query['id'], "rb") as fs:
|
||||
await response.write_eof(await fs.read())
|
||||
return response
|
11
FileStorage/views/upload_file.py
Normal file
11
FileStorage/views/upload_file.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from aiohttp import web
|
||||
|
||||
from FileStorage.sync import write_meta
|
||||
import aiofiles
|
||||
|
||||
|
||||
async def upload_file(request):
|
||||
file_id = await write_meta(request)
|
||||
async with aiofiles.open("data/" + str(file_id), "wb") as fs:
|
||||
await fs.write(await request.content.read())
|
||||
return web.json_response({"id": file_id})
|
Reference in New Issue
Block a user