75 lines
2.4 KiB
Python
75 lines
2.4 KiB
Python
import os
|
|
import cherrypy
|
|
import logging
|
|
from datetime import datetime, timedelta
|
|
from photoapp.library import PhotoLibrary
|
|
from photoapp.types import Photo, PhotoSet, Tag, TagItem, PhotoStatus, User
|
|
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
|
from sqlalchemy import desc
|
|
from sqlalchemy import func, and_, or_
|
|
from photoapp.common import pwhash
|
|
import math
|
|
from urllib.parse import urlparse
|
|
from photoapp.utils import mime2ext, auth, require_auth, photo_auth_filter, slugify
|
|
from photoapp.dbutils import db
|
|
|
|
|
|
class PhotosApi(object):
|
|
def __init__(self):
|
|
self.v1 = PhotosApiV1()
|
|
|
|
|
|
class PhotosApiV1(object):
|
|
def __init__(self):
|
|
pass
|
|
|
|
@cherrypy.expose
|
|
def index(self):
|
|
yield f"<plaintext>hello, this is the api. my database is: {db}\n"
|
|
|
|
@cherrypy.expose
|
|
def upload(self, files, meta):
|
|
pass
|
|
|
|
@cherrypy.expose
|
|
@cherrypy.tools.json_out()
|
|
def byhash(self, sha):
|
|
f = db.query(Photo).filter(Photo.hash == sha).first()
|
|
if not f:
|
|
raise cherrypy.HTTPError(404)
|
|
return f.to_json()
|
|
|
|
@cherrypy.expose
|
|
@cherrypy.tools.json_out()
|
|
def set(self, uuid):
|
|
s = db.query(PhotoSet).filter(PhotoSet.uuid == uuid).first()
|
|
if not s:
|
|
raise cherrypy.HTTPError(404)
|
|
return s.to_json()
|
|
|
|
@cherrypy.expose
|
|
def download(self, uuid):
|
|
f = db.query(Photo).filter(Photo.uuid == uuid).first()
|
|
if not f:
|
|
raise cherrypy.HTTPError(404)
|
|
return cherrypy.lib.static.serve_file(os.path.abspath(os.path.join("./library", f.path)),
|
|
f.format)#TODO no hardcode path
|
|
|
|
@cherrypy.expose
|
|
@cherrypy.tools.json_out()
|
|
def user(self, username=None, password_hash=None):
|
|
if username is None: # list all users
|
|
return [u.to_json() for u in db.query(User).all()]
|
|
elif username and cherrypy.request.method == "DELETE": # delete user
|
|
u = db.query(User).filter(User.name == username).first()
|
|
if not u:
|
|
raise cherrypy.HTTPError(404)
|
|
db.delete(u)
|
|
elif username and password_hash: # create/update user
|
|
u = db.query(User).filter(User.name == username).first()
|
|
if u:
|
|
u.password = password_hash
|
|
else:
|
|
db.add(User(name=username, password=password_hash))
|
|
return "ok"
|