Mercurial > libervia-web
changeset 1352:d100d3a07dd7
browser (photos/album): change cover on "cover" button click:
when "cover" button is clicked, image thumbnail is downloaded with ajax to get a blob, which
is then uploaded via HTTP Upload and used as cover.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 05 Sep 2020 21:59:38 +0200 |
parents | 2c3bc7284992 |
children | d1032f9ece5b |
files | libervia/pages/photos/album/_browser/__init__.py |
diffstat | 1 files changed, 74 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/libervia/pages/photos/album/_browser/__init__.py Sat Sep 05 21:59:23 2020 +0200 +++ b/libervia/pages/photos/album/_browser/__init__.py Sat Sep 05 21:59:38 2020 +0200 @@ -1,11 +1,15 @@ -from browser import document, window, bind, html, DOMNode +from browser import document, window, bind, html, DOMNode, aio from javascript import JSON from bridge import Bridge +from aio_bridge import Bridge as AIOBridge from template import Template import dialog from slideshow import SlideShow from invitation import InvitationManager import alt_media_player +# we use tmp_aio because `blob` is not handled in Brython's aio +import tmp_aio + cache_path = window.cache_path files_service = window.files_service @@ -15,6 +19,7 @@ except AttributeError: pass bridge = Bridge() +aio_bridge = AIOBridge() alt_media_player.install_if_needed() @@ -167,6 +172,72 @@ cancel_cb=lambda evt, notif_elt: delete_cancel(evt, notif_elt, item_elt, item), ) +# cover + +async def cover_ok(evt, notif_elt, item_elt, item): + # we first need to get a blob of the image + img_elt = item_elt.select_one("img") + # the simplest way is to download it + r = await tmp_aio.ajax("GET", img_elt.src, "blob") + if r.status != 200: + dialog.notification.show( + f"can't retrieve cover: {r.status}: {r.statusText}", + level="error" + ) + return + img_blob = r.response + # now we'll upload it via HTTP Upload, we need a slow + img_name = img_elt.src.rsplit('/', 1)[-1] + img_size = img_blob.size + + slot = await aio_bridge.fileHTTPUploadGetSlot( + img_name, + img_size, + '', + files_service + ) + get_url, put_url, headers = slot + # we have the slot, we can upload image + r = await tmp_aio.ajax("PUT", put_url, "", img_blob) + if r.status != 201: + dialog.notification.show( + f"can't upload cover: {r.status}: {r.statusText}", + level="error" + ) + return + extra = {"thumb_url": get_url} + album_name = files_path.rsplit('/', 1)[-1] + await aio_bridge.interestsRegisterFileSharing( + files_service, + "photos", + "", + files_path, + album_name, + JSON.stringify(extra), + ) + dialog.notification.show("Album cover has been changed") + + +def cover_cancel(evt, notif_elt, item_elt, item): + notif_elt.remove() + item_elt.classList.remove("selected_for_action") + + +def on_cover(evt): + evt.stopPropagation() + target = evt.currentTarget + item_elt = DOMNode(target.elt.closest('.item')) + item_elt.classList.add("selected_for_action") + item = JSON.parse(item_elt.dataset.item) + dialog.Confirm( + f"use {item['name']!r} for this album cover?", + ok_label="use as cover", + ).show( + ok_cb=lambda evt, notif_elt: aio.run(cover_ok(evt, notif_elt, item_elt, item)), + cancel_cb=lambda evt, notif_elt: cover_cancel(evt, notif_elt, item_elt, item), + ) + + # slideshow @bind(".photo_thumb_click", "click") @@ -210,6 +281,8 @@ for elt in document.select('.action_delete'): elt.bind("click", on_delete) +for elt in document.select('.action_cover'): + elt.bind("click", on_cover) # manage