Skip to content

Commit

Permalink
Bug fixes and improvements in file uploader component
Browse files Browse the repository at this point in the history
  • Loading branch information
essenciary committed Oct 16, 2024
1 parent 17ff66f commit cfdb360
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StippleUI"
uuid = "a3c5d34a-b254-4859-a8fa-b86abb7e84a3"
authors = ["Adrian Salceanu <[email protected]>"]
version = "0.23.4"
version = "0.23.5"

[deps]
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
Expand Down
42 changes: 42 additions & 0 deletions demos/uploader_2/app.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module App

using GenieFramework
@genietools

const UPLOADS_FOLDER = "public/uploads"
Genie.Watch.unwatch(UPLOADS_FOLDER)

@app begin
@event rejected begin
@info "rejected"
@notify("File rejected")
end

@event uploaded begin
@info "File uploaded"
end

@event finished begin
@info "Upload finished"
end

@event failed begin
@info "Upload failed"
@notify("File upload failed. Please try again.")
end

@onchange fileuploads begin
uploaded_file = UploadedFile(fileuploads)
try
cp(uploaded_file, UPLOADS_FOLDER; force = true)
catch e
@error "Error processing file: $e"
@notify("Error processing file: $(uploaded_file.name)")
# rethrow(e)
end
end
end

@page("/", "app.jl.html")

end
6 changes: 6 additions & 0 deletions demos/uploader_2/app.jl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<q-uploader label="Upload files" hide-upload-btn="" auto-upload=""
v-on:failed="function(event) { handle_event(event, 'failed') }"
v-on:uploaded="function(event) { handle_event(event, 'uploaded') }" no-thumbnails=""
v-on:finish="function(event) { handle_event(event, 'finished') }" :url="'/____/upload/' + channel_"
v-on:rejected="function(event) { handle_event(event, 'rejected') }" max-files="10" multiple="" accept="*/*"
:max-file-size="10 * 1024 * 1024" id="i6xi"></q-uploader>
89 changes: 85 additions & 4 deletions src/Uploaders.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module Uploaders

using Genie, Stipple, StippleUI, StippleUI.API
using Dates
import Genie.Renderer.Html: HTMLString, normal_element, register_normal_element

export uploader
export uploader, UploadedFile

register_normal_element("q__uploader", context = @__MODULE__)

Expand All @@ -24,15 +25,18 @@ function __init__()

route("/____/upload/:channel", method = POST) do
for f in Genie.Requests.filespayload()
uf = UploadedFile(tempname(), f[2].name, params(:channel))
tmpdir = mktempdir(; cleanup = true)
tmpfile = mktemp(tmpdir; cleanup = true)

try
write(uf.tmppath, f[2].data)
write(tmpfile[2], f[2].data)
close(tmpfile[2])
catch e
@error "Error saving uploaded file: $e"
rethrow(e)
end

uf = UploadedFile(tmpfile[1], f[2].name, params(:channel))
push_uploaded_files(uf)

for h in upload_handlers
Expand Down Expand Up @@ -62,7 +66,7 @@ function push_uploaded_files(uf::UploadedFile)

Stipple._push!(:fileuploads => filedict, uf.channel)

# # this won't be broadcasted back to the server so we need to do it manually
# this won't be broadcasted back to the server so we need to do it manually
Stipple.WEB_TRANSPORT[].broadcast(
Genie.WebChannels.tagbase64encode(""">eval:Genie.WebChannels.sendMessageTo(
window.CHANNEL,
Expand Down Expand Up @@ -153,4 +157,81 @@ function uploader(args...;
q__uploader(args...; kw([kws..., kwargs...])...)
end


# API utilities to work with uploaded files

"""
UploadedFile(d::T) where T <: AbstractDict
Create an UploadedFile object from a dictionary
Arguments
---------
* `d::T` - Dictionary with keys `path`, `name`, and `channel`
"""
function UploadedFile(d::T) where T <: AbstractDict
UploadedFile(d["path"], d["name"], d["channel"])
end


"""
cp(uf::UploadedFile, dest::String; filename::Union{String,Nothing} = nothing, create_dist::Bool = true, force::Bool = false)
Copy an uploaded file to a destination folder
Arguments
---------
* `uf::UploadedFile` - Uploaded file object
* `dest::String` - Destination folder
* `filename::Union{String,Nothing}` - New filename (optional, defaults to the original filename)
* `create_dist::Bool` - Create destination folder if it doesn't exist
* `force::Bool` - Overwrite existing files
"""
function Base.cp(uf::UploadedFile, dest::String; filename::Union{String,Nothing} = nothing, create_dist::Bool = true, force::Bool = false)
create_dist && (isdir(dest) || mkpath(dest))
isnothing(filename) && (filename = uf.name)

isfile(uf.tmppath) || error("File not found: $(uf.tmppath)")

file_path_dest = joinpath(dest, filename)
Base.Filesystem.cp(uf.tmppath, file_path_dest; force = force)

return file_path_dest
end


"""
mv(uf::UploadedFile, dest::String; filename::Union{String,Nothing} = nothing, create_dist::Bool = true, force::Bool = false)
Move an uploaded file to a destination folder
Arguments
---------
* `uf::UploadedFile` - Uploaded file object
* `dest::String` - Destination folder
* `filename::Union{String,Nothing}` - New filename (optional, defaults to the original filename)
* `create_dist::Bool` - Create destination folder if it doesn't exist
* `force::Bool` - Overwrite existing files
"""
function Base.mv(uf::UploadedFile, dest::String; filename::Union{String,Nothing} = nothing, create_dist::Bool = true, force::Bool = false)
result = cp(uf, dest; filename = filename, create_dist = create_dist, force = force)
rm(uf.tmppath)

return result
end


"""
rm(uf::UploadedFile)
Remove the temporary file associated with an UploadedFile object
Arguments
---------
* `uf::UploadedFile` - Uploaded file object
"""
function Base.rm(uf::UploadedFile)
isfile(uf.tmppath) && rm(uf.tmppath)
end

end

2 comments on commit cfdb360

@essenciary
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/117415

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.23.5 -m "<description of version>" cfdb3603cf37526a6ccd49db5edc52a0dd1c7a7f
git push origin v0.23.5

Please sign in to comment.