Last active
December 17, 2015 07:03
-
-
Save jolle-c/9957187 to your computer and use it in GitHub Desktop.
Type for Lasso 9 to facilitate handling of uploaded files.
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
[ | |
/**! | |
jc_fileuploads | |
local(myfile = jc_fileuploads(1)) // the integer is optional and will if provided restrict the method to the n:th count of uploaded files | |
#myfile -> size // how many files that where uploaded or handled by the type | |
#myfile -> contenttype // or #myfile -> contenttype(n) when targeting the n:th file // for example image/jpeg | |
#myfile -> fieldname // #myfile -> fieldname(n) | |
#myfile -> filesize // #myfile -> filesize(n) | |
#myfile -> filename // #myfile -> filename(n) | |
#myfile -> tmpfilename // #myfile -> tmpfilename(n) | |
#myfile -> suffix // #myfile -> suffix(n) | |
#myfile -> save('/path/to/file/filename.sfx') | |
or | |
#myfile -> save('/path/to/file/filename.sfx', -overwrite, -count = n) | |
An actual file can also be retrieved as a file object from jc_fileuploads using | |
#myfile -> file | |
or | |
#myfile -> file(n) | |
Short hand examples | |
jc_fileuploads -> file // will return the uploaded file as a file object | |
jc_fileuploads -> save('/path/to/file/filename.sfx') | |
jc_fileuploads also works with query expressions | |
local(myfiles = jc_fileuploads) | |
(with file in #myfiles | |
let name = #file -> filename | |
let filesize = #file -> filesize | |
select 'The file ' + #name + ' was uploaded with size ' + #filesize) -> join('<br />') | |
CHANGE HISTORY | |
2015-12-17 JC Changed suffix method to use splitextension | |
2014-11-03 JC Rewritten to support query expressions | |
2014-10-22 JC Fixed save signatures to adjust for 9.3 issues (Thanks Brad!) | |
2013-11-01 JC Minor tweaks to data names and calls | |
2012-07-31 JC Corrected the abortion of the with loop when type is called for only one | |
2011-10-24 JC First version | |
*/ | |
define jc_fileuploads => type { | |
trait { import trait_queriable } | |
data private fileinfo = map | |
data private filecount = 0 | |
public oncreate(count::integer = -1) => { | |
local(fup_tmp = map) | |
local(fups = web_request -> fileuploads) | |
local(_count = #count > 0 ? #count | #fups -> size) | |
local(lcount = 0) | |
with fup in #fups | |
where #lcount < #_count | |
do { | |
#lcount += 1 | |
#fup_tmp = jc_fileupload | |
with upl in #fup | |
do { | |
#fup_tmp -> insert(#upl -> first = #upl->second) | |
} | |
.fileinfo -> insert(#lcount = #fup_tmp) | |
} | |
.filecount = .fileinfo -> size | |
} // oncreate | |
public forEach() => { | |
.fileinfo -> foreach => { | |
givenblock(#1) | |
} | |
} | |
public size() => .filecount | |
public contenttype(count::integer = 1) => { | |
if(#count <= .filecount) => { | |
return .fileinfo -> find(#count) -> contenttype | |
} | |
} | |
public fieldname(count::integer = 1) => { | |
if(#count <= .filecount) => { | |
return .fileinfo -> find(#count) -> fieldname | |
} | |
} | |
public filesize(count::integer = 1) => { | |
if(#count <= .filecount) => { | |
return .fileinfo -> find(#count) -> filesize | |
} | |
} | |
public tmpfilename(count::integer = 1) => { | |
if(#count <= .filecount) => { | |
return .fileinfo -> find(#count) -> tmpfilename | |
} | |
} | |
public filename(count::integer = 1) => { | |
if(#count <= .filecount) => { | |
return .fileinfo -> find(#count) -> filename | |
} | |
} | |
public suffix(count::integer = 1) => { | |
if(#count <= .filecount) => { | |
return .fileinfo -> find(#count) -> suffix | |
} | |
} | |
public save(path::string, overwrite::boolean, count::integer = 1) => { | |
if(#count<= .filecount) => { | |
.fileinfo -> find(#count) -> save(#path, #overwrite) | |
} | |
} | |
public save(path::string, -overwrite::boolean = false, -count::integer = | |
1) => .save(#path, #overwrite, #count) | |
public file(count::integer = 1) => { | |
if(#count <= .filecount) => { | |
return .fileinfo -> find(#count) -> file | |
} | |
} | |
} | |
define jc_fileupload => type { | |
data private fileinfo = map | |
public oncreate(...) => { | |
#rest? #rest->forEach => { .fileinfo -> insert(#1) } | |
} | |
public insert(item::pair) => { | |
.fileinfo -> insert(#item) | |
} | |
public asstring => { | |
return .fileinfo | |
} | |
public contenttype => { | |
return .fileinfo -> find('contenttype') | |
} | |
public fieldname => { | |
return .fileinfo -> find('fieldname') | |
} | |
public filesize => { | |
return .fileinfo -> find('filesize') | |
} | |
public tmpfilename => { | |
return .fileinfo -> find('tmpfilename') | |
} | |
public filename => { | |
return .fileinfo -> find('filename') | |
} | |
public suffix => { | |
return .fileinfo -> find('filename') -> splitextension -> value | |
} | |
public save(path::string, overwrite::boolean) => { | |
file(.fileinfo -> find('tmpfilename')) -> copyto(#path, #overwrite) | |
} | |
public save(path::string, -overwrite::boolean = false) => .save(#path, #overwrite) | |
public file => { | |
return file(.fileinfo -> find('tmpfilename')) | |
} | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment