mirror of
https://github.com/minetest/contentdb.git
synced 2025-01-09 06:27:38 +01:00
Add single API to upload cover image
This commit is contained in:
parent
dfef268b05
commit
958f92fd63
@ -304,7 +304,7 @@ def create_screenshot(token: APIToken, package: Package):
|
|||||||
if file is None:
|
if file is None:
|
||||||
error(400, "Missing 'file' in multipart body")
|
error(400, "Missing 'file' in multipart body")
|
||||||
|
|
||||||
return api_create_screenshot(token, package, data["title"], file)
|
return api_create_screenshot(token, package, data["title"], file, isYes(data.get("is_cover_image")))
|
||||||
|
|
||||||
|
|
||||||
@bp.route("/api/packages/<author>/<name>/screenshots/<int:id>/")
|
@bp.route("/api/packages/<author>/<name>/screenshots/<int:id>/")
|
||||||
|
@ -69,13 +69,13 @@ def api_create_zip_release(token: APIToken, package: Package, title: str, file,
|
|||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
def api_create_screenshot(token: APIToken, package: Package, title: str, file, reason="API"):
|
def api_create_screenshot(token: APIToken, package: Package, title: str, file, is_cover_image: bool, reason="API"):
|
||||||
if not token.canOperateOnPackage(package):
|
if not token.canOperateOnPackage(package):
|
||||||
error(403, "API token does not have access to the package")
|
error(403, "API token does not have access to the package")
|
||||||
|
|
||||||
reason += ", token=" + token.name
|
reason += ", token=" + token.name
|
||||||
|
|
||||||
ss : PackageScreenshot = guard(do_create_screenshot)(token.owner, package, title, file, reason)
|
ss : PackageScreenshot = guard(do_create_screenshot)(token.owner, package, title, file, is_cover_image, reason)
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"success": True,
|
"success": True,
|
||||||
|
@ -87,7 +87,7 @@ def create_screenshot(package):
|
|||||||
form = CreateScreenshotForm()
|
form = CreateScreenshotForm()
|
||||||
if form.validate_on_submit():
|
if form.validate_on_submit():
|
||||||
try:
|
try:
|
||||||
do_create_screenshot(current_user, package, form.title.data, form.fileUpload.data)
|
do_create_screenshot(current_user, package, form.title.data, form.fileUpload.data, False)
|
||||||
return redirect(package.getURL("packages.screenshots"))
|
return redirect(package.getURL("packages.screenshots"))
|
||||||
except LogicError as e:
|
except LogicError as e:
|
||||||
flash(e.message, "danger")
|
flash(e.message, "danger")
|
||||||
|
@ -234,6 +234,7 @@ curl -X DELETE https://content.minetest.net/api/packages/username/name/releases/
|
|||||||
* Body is multipart form data.
|
* Body is multipart form data.
|
||||||
* `title`: human-readable name for the screenshot, shown as a caption and alt text.
|
* `title`: human-readable name for the screenshot, shown as a caption and alt text.
|
||||||
* `file`: multipart file to upload, like `<input type=file>`.
|
* `file`: multipart file to upload, like `<input type=file>`.
|
||||||
|
* `is_cover_image`: set cover image to this.
|
||||||
* DELETE `/api/packages/<username>/<name>/screenshots/<id>/` (Delete)
|
* DELETE `/api/packages/<username>/<name>/screenshots/<id>/` (Delete)
|
||||||
* Requires authentication.
|
* Requires authentication.
|
||||||
* Deletes screenshot.
|
* Deletes screenshot.
|
||||||
@ -254,6 +255,11 @@ Examples:
|
|||||||
curl -X POST https://content.minetest.net/api/packages/username/name/screenshots/new/ \
|
curl -X POST https://content.minetest.net/api/packages/username/name/screenshots/new/ \
|
||||||
-H "Authorization: Bearer YOURTOKEN" \
|
-H "Authorization: Bearer YOURTOKEN" \
|
||||||
-F title="My Release" -F file=@path/to/screnshot.png
|
-F title="My Release" -F file=@path/to/screnshot.png
|
||||||
|
|
||||||
|
# Create screenshot and set it as the cover image
|
||||||
|
curl -X POST https://content.minetest.net/api/packages/username/name/screenshots/new/ \
|
||||||
|
-H "Authorization: Bearer YOURTOKEN" \
|
||||||
|
-F title="My Release" -F file=@path/to/screnshot.png -F is_cover_image="true"
|
||||||
|
|
||||||
# Delete screenshot
|
# Delete screenshot
|
||||||
curl -X DELETE https://content.minetest.net/api/packages/username/name/screenshots/3/ \
|
curl -X DELETE https://content.minetest.net/api/packages/username/name/screenshots/3/ \
|
||||||
|
@ -9,7 +9,7 @@ from app.utils import addNotification, addAuditLog
|
|||||||
from app.utils.image import get_image_size
|
from app.utils.image import get_image_size
|
||||||
|
|
||||||
|
|
||||||
def do_create_screenshot(user: User, package: Package, title: str, file, reason: str = None):
|
def do_create_screenshot(user: User, package: Package, title: str, file, is_cover_image: bool, reason: str = None):
|
||||||
thirty_minutes_ago = datetime.datetime.now() - datetime.timedelta(minutes=30)
|
thirty_minutes_ago = datetime.datetime.now() - datetime.timedelta(minutes=30)
|
||||||
count = package.screenshots.filter(PackageScreenshot.created_at > thirty_minutes_ago).count()
|
count = package.screenshots.filter(PackageScreenshot.created_at > thirty_minutes_ago).count()
|
||||||
if count >= 20:
|
if count >= 20:
|
||||||
@ -47,6 +47,10 @@ def do_create_screenshot(user: User, package: Package, title: str, file, reason:
|
|||||||
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
if is_cover_image:
|
||||||
|
package.cover_image = ss
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
return ss
|
return ss
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user