New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow file uploads with attribute #404
Closed
wout
wants to merge
5
commits into
luckyframework:master
from
wout:allow-file-uploads-with-attribute
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9ecc4d8
Add file_attribute macro, uploadable and uploadable_file.
wout 20d857d
Add test for upload operation.
wout e546e11
Allow uploaded file as an attribute in operations.
wout d337e2a
Whitespace style fixes in define_attribute module.
wout c130dc3
Merge branch 'master' into allow-file-uploads-with-attribute
wout File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class UploadParams < Avram::Params | ||
include Avram::Paramable | ||
|
||
@uploads : Hash(String, UploadedFile) = {} of String => UploadedFile | ||
|
||
def initialize(@uploads) | ||
end | ||
|
||
def nested_file?(key) : Hash(String, UploadedFile) | ||
@uploads | ||
end | ||
|
||
def nested_file(key) : Hash(String, UploadedFile) | ||
@uploads | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class UploadedFile | ||
include Avram::Uploadable | ||
|
||
def initialize(filename : String) | ||
@name = "part.name" | ||
@tempfile = File.tempfile(@name) { |file| File.write(file.path, "tmp") } | ||
@metadata = HTTP::FormData::FileMetadata.new(filename) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Include this module in classes that represent an uploaded file from a form | ||
module Avram::Uploadable | ||
getter name : String | ||
getter tempfile : File | ||
getter metadata : HTTP::FormData::FileMetadata | ||
|
||
macro included | ||
# :nodoc: | ||
def initialize(file : Avram::Uploadable) | ||
@name = file.name | ||
@tempfile = file.tempfile | ||
@metadata = file.metadata | ||
end | ||
end | ||
|
||
# Returns the path of the File as a String | ||
# | ||
# ``` | ||
# uploaded_file_object.path # => String | ||
# ``` | ||
def path : String | ||
@tempfile.path | ||
end | ||
|
||
# Returns the original file name as a String | ||
# | ||
# ``` | ||
# uploaded_file_object.filename # => String | ||
# ``` | ||
def filename : String | ||
metadata.filename.to_s | ||
end | ||
|
||
# If no file was selected in the form's file input, this will return `true` | ||
# | ||
# ``` | ||
# uploaded_file_object.blank? # => Bool | ||
# ``` | ||
def blank? : Bool | ||
filename.blank? | ||
end | ||
|
||
module Lucky | ||
include Avram::Type | ||
|
||
def parse(value : Avram::Uploadable) | ||
SuccessfulCast(Avram::Uploadable).new(value) | ||
end | ||
|
||
def parse(value : String?) | ||
FailedCast.new | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think it might be possible to abstract this metadata somewhat?
I love your approach of being able to use this module outside of
Lucky::UploadedFile
, but this means it could be used for files that arrived some other way, not necessarily via HTTP. These could of course simply construct their ownHTTP::FormData::FileMetadata
, but it would feel a bit awkward imho.Also, avram is supposed to be a general purpose ORM. Seeing things related to HTTP here is at least a little bit confusing.
Maybe it might suffice to simply add the attributes from
HTTP::FormData::FileMetadata
that are relevant here directly (possibly size and the different timestamps). Personally I would be fine with removing this altogether. At this point we already have the complete file to determine the size reliably and I have never looked at what timestamps the browser sends with files before.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good point! We only need it for the original
filename
. I think themetadata
property can move back toLucky::UploadedFile
andAvram::Uploadable
can declare an interface forfilename
:So that includers can implement it however they like:
I think we'll also need to rethink the
name
property as well (as discussed in luckyframework/lucky#1147). I can't seem to find a place where it's actually used. For clarity, at least we could change it topartname
, and raise a compile-time error on thename
method referencing bothfilename
andpartname
.