Genie Discord forum

Author Avataryakir12
12/1/2023, 4:29:38 PM

In my app, users can download (tarballed) files from the server.

Right now, my implementation looks like this:

btn(class = "q-mt-lg", "Download data", color = "primary", href="data", download=string(round(now(), Second(1)), ".tar"))

where data is a route that returns a tarball with the files compressed in it.

I have two questions:

  1. Is there a "downloader" component equivalent to the already existing uploader component?
  2. Once the user downloaded the data, I would like to have the downloaded files deleted. Is there some kind of post-action I can define in the implementation that deletes the files only after a successful download? Something like btn(..., onsuccess = my_delete_files_function())
Author AvatarPere
12/1/2023, 5:08:13 PM

There is no download component that I know of, and I just found out that you can add href and download properties to a btn πŸ˜„

Today @hhaensel added a new Stipple plugin to trigger downloads from the backend

https://github.com/GenieFramework/StippleDownloads.jl

You could use this to serve the file and delete it right after

Author Avatarhhaensel
12/1/2023, 10:57:26 PM

@yakir12 It's in the process of being registered, so you need to dev it if you can't wait to try it out. If you can't figure out how to use it for your case, don't hesitate to open an issue. If you think, your use case should be mentioned in the README let me know

Author Avatarhhaensel
12/1/2023, 10:58:41 PM

A static file ca be passed to data by read(filename)...

Author Avatarhhaensel
12/1/2023, 10:59:19 PM

I should probably add a method for passing IOBuffers

Author Avatarhhaensel
12/1/2023, 11:00:08 PM

It would save you a take!()

Author Avataraww
12/2/2023, 8:38:07 AM

That’s really great! I’m wondering how to implement that when we have 500+ concurrent users, each file should have a distinct name depending on a session correct? I suppose this could be done by event"_client"?

Author Avatarhhaensel
12/2/2023, 9:14:11 AM

That's not completely correct. The _client is only there to differentiate between different clients with the same channel,say the same user with the same browser logged in twice

Author Avatarhhaensel
12/2/2023, 9:15:20 AM

All users can receive different files under the identical name. But you are free to chose it.

Author Avataryakir12
12/3/2023, 3:40:54 PM

This worked very nicely. Thanks a lot!

Here's my MWE to test my specific needs (i.e. deleting the files after a successful download):

using Stipple, Stipple.ReactiveTools
using StippleUI
using StippleDownloads

import Stipple.opts

@app begin
    @event download_file begin
        file = tempname(".", cleanup=false)
        open(file, "w") do io
            write(io, rand(UInt8, 1000))
        end
        try
            download_binary(__model__, read(rand(Bool) ? file : wrong_variable_name), "binary"; client = event["_client"])
            @info "download worked, deleting file"
            rm(file)
        catch ex
            @warn ex
            @warn "download failed, not delteing file"
        end
    end
end

function ui()
    row(cell(class = "st-module", [
        row([
            cell(btn(class = "q-ml-lg", "Download and delete file", icon = "download", @on(:click, :download_file, :addclient), color = "primary", nocaps = true))
        ])
    ]))
end

@page("/", ui)

up(open_browser = true)
Author Avatarhhaensel
12/4/2023, 7:54:01 AM

I answered in the issue; basically you have to move the event handler out of the @appmacro ...

Author Avataryakir12
12/4/2023, 8:39:38 AM

yap, and it worked and I closed the issue. Perfect! Thank you so much πŸ™‚