add support for copying photos
This commit is contained in:
parent
efb4019c82
commit
e7ae240555
106
example.py
106
example.py
|
@ -32,13 +32,16 @@ def authed_endpoint(f):
|
|||
authtok = request.headers.get('Authorization')
|
||||
|
||||
if authtok is None:
|
||||
return {"error": "No token provided"}, 401
|
||||
return {
|
||||
"error": "unauthorized",
|
||||
"error_description": "An auth token was not provided"
|
||||
}, 401
|
||||
|
||||
auth = requests.get("https://tokens.indieauth.com/token", headers={
|
||||
"Authorization": authtok, "Accept": "application/json"}).json()
|
||||
|
||||
if auth['me'] not in PERMITTED_DOMAIN:
|
||||
return {"error": f"User {auth['me']} not permitted to post here"}, 403
|
||||
return {"error": "forbidden", "error_description": f"User {auth['me']} not permitted to post here"}, 403
|
||||
|
||||
return f(*args, *kwargs)
|
||||
|
||||
|
@ -67,42 +70,79 @@ def req():
|
|||
doc = request.form.to_dict(flat=True)
|
||||
|
||||
if 'name' in doc:
|
||||
entry_type = "posts"
|
||||
entry_type = "post"
|
||||
else:
|
||||
entry_type = "micros"
|
||||
|
||||
entry_type = "note"
|
||||
|
||||
now = datetime.now()
|
||||
now_ts = int(time.mktime(now.timetuple()))
|
||||
|
||||
url = os.path.join("/",entry_type,now.strftime("%Y/%m/%d"), str(now_ts))
|
||||
file_path = os.path.join(os.environ.get('CONTENT_PREFIX'), entry_type, now.strftime("%Y/%m/%d"), str(now_ts) + ".md")
|
||||
url = os.path.join("/", entry_type + "s",
|
||||
now.strftime("%Y/%m/%d"), str(now_ts))
|
||||
|
||||
if 'name' in doc:
|
||||
slug = slugify(doc['name']) + str(now_ts)
|
||||
else:
|
||||
slug = str(now_ts)
|
||||
|
||||
file_path = os.path.join(os.environ.get(
|
||||
'CONTENT_PREFIX'), entry_type + "s", now.strftime("%Y/%m/%d"), slug + ".md")
|
||||
|
||||
frontmatter = {
|
||||
"url":url,
|
||||
"url": url,
|
||||
"type": entry_type,
|
||||
"date": now.isoformat(sep='T'),
|
||||
}
|
||||
|
||||
if 'category' in doc:
|
||||
categories = [doc['category']]
|
||||
else:
|
||||
categories = request.form.getlist('category[]')
|
||||
|
||||
if 'name' in doc:
|
||||
frontmatter['title'] = doc['name']
|
||||
|
||||
if len(categories) > 0:
|
||||
frontmatter['categories'] = categories
|
||||
|
||||
|
||||
frontmatter['tags'] = categories
|
||||
|
||||
frontmatter_str = yaml.dump(frontmatter)
|
||||
|
||||
content = base64.encodestring(f"---\n{frontmatter_str}\n---\n\n{doc['content']}".encode("utf8")).decode("utf8")
|
||||
if 'photo' in doc:
|
||||
|
||||
if os.environ.get('MICROPUB_IMAGE_STRATEGY') == 'copy':
|
||||
# download the photo
|
||||
r = requests.get(doc['photo'])
|
||||
|
||||
# generate local filename
|
||||
filename = os.path.join(os.environ.get('MICROPUB_MEDIA_PATH'), now.strftime("%Y/%m/%d"), str(now_ts) + ".jpg")
|
||||
photo_url = os.path.join(os.environ.get('MICROPUB_MEDIA_URL_PREFIX'), now.strftime("%Y/%m/%d"), str(now_ts) + ".jpg")
|
||||
|
||||
# make directory if needed
|
||||
os.makedirs(os.path.dirname(filename))
|
||||
|
||||
with open(filename, 'wb') as f:
|
||||
f.write(r.content)
|
||||
|
||||
# elif os.environ.get('MICROPUB_IMAGE_STRATEGY') == 'gitea':
|
||||
|
||||
else:
|
||||
photo_url = doc['photo']
|
||||
|
||||
|
||||
docstr = f"![image]({photo_url}) \n\n {doc['content']}"
|
||||
else:
|
||||
docstr = doc['content']
|
||||
|
||||
content = base64.encodestring(
|
||||
f"---\n{frontmatter_str}\n---\n\n{docstr}".encode("utf8")).decode("utf8")
|
||||
|
||||
api = get_api_client()
|
||||
|
||||
body = giteapy.CreateFileOptions(content=content)
|
||||
|
||||
try:
|
||||
r = api.repo_create_file(os.environ.get('GITEA_REPO_OWNER'), os.environ.get('GITEA_REPO_NAME'), file_path, body)
|
||||
r = api.repo_create_file(os.environ.get(
|
||||
'GITEA_REPO_OWNER'), os.environ.get('GITEA_REPO_NAME'), file_path, body)
|
||||
|
||||
return Response(status=202, headers={"Location": url})
|
||||
|
||||
|
@ -114,8 +154,46 @@ def req():
|
|||
# return {"hello": "world"}
|
||||
|
||||
|
||||
@app.route('/', methods=['GET'])
|
||||
@app.route("/", methods=['GET'])
|
||||
@authed_endpoint
|
||||
def index():
|
||||
|
||||
if request.args.get('q') == 'config':
|
||||
return {
|
||||
"media-endpoint": "/micropub/media",
|
||||
"syndicate-to": [
|
||||
{
|
||||
"uid": "mastodon",
|
||||
"name": "Mastodon"
|
||||
}
|
||||
],
|
||||
"post-types": [
|
||||
{
|
||||
"type": "note",
|
||||
"name": "Note"
|
||||
},
|
||||
{
|
||||
"type": "article",
|
||||
"name": "Blog Post"
|
||||
},
|
||||
{
|
||||
"type": "photo",
|
||||
"name": "Photo"
|
||||
},
|
||||
{
|
||||
"type": "reply",
|
||||
"name": "Reply"
|
||||
},
|
||||
{
|
||||
"type": "bookmark",
|
||||
"name": "Bookmark"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
@app.route('/form', methods=['GET'])
|
||||
def authform():
|
||||
return """
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
|
Loading…
Reference in New Issue