Skip to content

Commit

Permalink
added proper file uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
kfarwell committed May 20, 2016
1 parent 556aa63 commit 385f046
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 42 deletions.
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
* fix tag overflow when there are many tags at small horizontal resolutions
* add proper file uploads
* improve trending tag algorithm
26 changes: 8 additions & 18 deletions apps/posts/app.rc
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,14 @@ fn posts_init {
touch -t $date $postd/tags

if(! ~ '' $"post_arg_file) {
# strip ?args and #anchors
if(basename $post_arg_file | grep -s '\?')
post_arg_file=`{basename -d $post_arg_file}^/`{basename $post_arg_file |
sed 's/\?.*//'}
if(basename $post_arg_file | grep -s '#')
post_arg_file=`{basename -d $post_arg_file}^/`{basename $post_arg_file |
sed 's/#.*//'}

if(~ `{wget --spider $post_arg_file >[2=1] | grep '^Length:' |
sed 's/Length: //; s/ \(.*//' |
awk '{print ($1 < '$filesizelimit')}'} 1) {
ext=`{basename $post_arg_file |
sed 's/.*\.(.*)$/\1/; s/[^a-zA-Z0-9]//g'}
wget -O $postd/file.$ext $post_arg_file
exiftool '-all=' '-overwrite_original' $postd/file.$ext
echo $ext > $postd/filetype
touch -t $date $postd/file.* $postd/filetype
}
ext=`{basename $post_arg_file |
sed 's/.*\.(.*)$/\1/; s/[^a-zA-Z0-9]//g'}
mv /tmp/werc_file.*.data $postd/file.$ext
dprint $postd^file.$ext
dprint `{ls $postd^file.$ext}
exiftool '-all=' -overwrite_original -q $postd^file.$ext
echo $ext > $postd/filetype
touch -t $date $postd/file.* $postd/filetype
}

if(! ~ '' $"post_arg_password) {
Expand Down
29 changes: 10 additions & 19 deletions apps/posts/edit.tpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
% if(~ $#posts_users_only 0 || check_user $groups_allowed_posts) {
<noscript>
<div>
<form action="" method="POST">
<form action="" method="POST" enctype="multipart/form-data">
<div class="input-field">
<textarea name="comment" id="comment" class="materialize-textarea validate" required="" length="%($charlimit%)" maxlength="%($charlimit%)"></textarea>
<label for="comment">Message</label>
Expand All @@ -12,19 +12,15 @@
<label for="tags">Tags (comma separated)</label>
</div>
<!-- <div class="input-field file-field">
<div class="input-field file-field">
<div class="btn pink">
<i class="mdi mdi-attachment left"></i>
<span>File</span>
<input type="file">
<input type="file" name="file">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
<input class="file-path validate" type="text" placeholder="<%(`{echo $filesizelimit | humanize}%)">
</div>
</div> -->
<div class="input-field">
<input type="text" placeholder="https://example.com/image.png" name="file" id="file">
<label for="file">Attachment (<%(`{echo $filesizelimit | humanize}%))</label>
</div>
<div class="input-field">
Expand All @@ -39,7 +35,7 @@
</noscript>
<div id="modalpost" class="yesscript modal">
<form action="" method="POST" onsubmit="$('#postprog').show()">
<form action="" method="POST" enctype="multipart/form-data" onsubmit="$('#postprog').show()">
<div class="modal-content">
<div class="input-field">
<textarea name="comment" id="comment" class="materialize-textarea validate" required="" length="%($charlimit%)" maxlength="%($charlimit%)"></textarea>
Expand Down Expand Up @@ -75,20 +71,15 @@
</script>
<br /><a onclick="toggleAdvanced()" class="waves-effect btn-flat" style="padding: 0 0.75rem">Advanced <i class="mdi mdi-chevron-down" id="advancedArrow"></i></a>
<div id="advanced">
<!-- <div class="input-field file-field">
<div class="input-field file-field">
<div class="btn pink">
<i class="mdi mdi-attachment left"></i>
<span>File</span>
<input type="file">
<input type="file" name="file">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
<input class="file-path validate" type="text" placeholder="<%(`{echo $filesizelimit | humanize}%)">
</div>
</div> -->

<br /><br />
<div class="input-field">
<input type="text" placeholder="https://example.com/image.png" name="file" id="file">
<label for="file">Attachment (<%(`{echo $filesizelimit | humanize}%))</label>
</div>

<div class="input-field">
Expand Down
81 changes: 77 additions & 4 deletions bin/cgilib.rc
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ fn perm_redirect { http_redirect $1 '301 Moved Permanantly' }
fn post_redirect { http_redirect $1 '303 See Other' }


# Note: should check if content type is application/x-www-form-urlencoded?
# Should compare with http://www.shelldorado.com/scripts/cmds/urlgetopt.txt
fn load_post_args {
if(~ $REQUEST_METHOD POST && ~ $#post_args 0) {
fn x-www-form-urlencoded {
ifs='&
' for(pair in `{cat}) {
ifs='=' { pair=`{echo -n $pair} }
Expand All @@ -34,6 +31,82 @@ fn load_post_args {
ifs=() { $n=`{echo -n $pair(2)|urldecode|tr -d ''} }
}
pair=()
}

fn multipart-form-data {
cdd=`{pwd}
if(~ $#upload_dir 0)
upload_dir = /tmp
cd $upload_dir
post_args = ()
ifs='
'
#frmprefix = werc_^`{date | tr ' :' '__'} ^.$pid
frmprefix = werc_file
split -e `{echo -n $CONTENT_TYPE | sed 's/.*=//'} -f $frmprefix^.
for(upload in $frmprefix.*) {
process-mime-part < $upload
rm -f $upload
}
cd $cdd
}

fn add_post_arg {
flag p +
for(a in $post_args)
~ $1 $a && flag p -
if(flag p)
post_args=($post_args $1)
}

fn process-mime-part {
ifs='
'
meta = `{awk ' { s+=length+1} /Content/ { print } /^.$/ {exit } END { print s} ' $upload }
if(~ $meta *Content-Disp*) {
skip=$meta($#meta)
n = post_arg_^`{echo $meta | awk ' /Content-Disp/ { sub(/.* name="/, ""); sub(/".*/, ""); print } ' |tr -cd 'a-zA-Z0-9_'}
add_post_arg $n
b = `{ls -l $upload | awk -v 's=-'$skip ' { s+=$6; s-=2; printf "%d\n%d", s/512, s % 512} '}
{
if(! ~ $skip 0)
dd -bs $skip -count 1 > /dev/null
if(! ~ $b(1) 0)
dd -bs 512 -count $b(1)
if(! ~ $b(2) 0)
dd -bs $b(2) -count 1
} >[2] /dev/null | {
if(~ $meta *'Content-Disposition: form-data'*'filename="'*) {
f = `{echo $meta | awk ' /Content-Disp/ { sub(/.*filename="/, ""); sub(/".*/, ""); print } ' }
if(! ~ $#f 0) {
$n = ($$n $f)
n = $n^_filename
$n = ($$n $upload.data)
add_post_arg $n
ifs=$nl
cat > $upload.data
}
}
if not {
ifs=()
#v=`{cat | tr -d ^M}
v=`{cat}
$n = ($$n $v)
}
}
}
}

fn load_post_args {
if(~ $REQUEST_METHOD POST && ~ $#post_args 0) {
switch(`{echo $CONTENT_TYPE | sed 's/;.*//'}) {
case application/x-www-form-urlencoded
x-www-form-urlencoded
case multipart/form-data
multipart-form-data
case *
dprint unhandled "$CONTENT_TYPE"
}
}
if not
status='No POST or post args already loaded'
Expand Down

1 comment on commit 385f046

@kfarwell
Copy link
Member Author

Choose a reason for hiding this comment

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

Please sign in to comment.