rejetto forum

Software => HFS ~ HTTP File Server => Programmers corner => Topic started by: Mars on February 19, 2010, 11:34:16 PM

Title: only one line to add a user comment on file upload
Post by: Mars on February 19, 2010, 11:34:16 PM
Quote
 procedure addUploadResultsSymbols();
  var
    files: string;
    i: integer;
  begin
  if sectionName <> 'upload-results' then exit;
  files:='';
  for i:=0 to length(data.uploadResults)-1 do
    with data.uploadResults do
      files:=files+xtpl(tpl2use[ if_(reason='','upload-success','upload-failed') ],[
        '%item-name%', macroQuote(optUTF8(tpl2use, fn)),
        '%idx%', intToStr(i+1),
        '%item-url%', encodeURL(fn),
        '%item-size%', smartsize(size),
        '%item-resource%', f.resource+'\'+fn,
        '%reason%', optUTF8(tpl2use, reason),
        '%speed%', intToStr(speed)
      ]);
  addArray(md.table, ['%uploaded-files%', files]);
  end; // addUploadResultsSymbols

usage:

Quote
[upload-file]
<input name='fileupload%idx%' size='70' type='file'><input name='filecomment%idx%' size='20' type='text'><br>

[upload-success]
<li><a href='%item-url%'>%item-name%</a>: <b>{.!OK.}</b> --- %item-size%  ({.!Speed.} %speed% KB/s) comment  = {.postvar|filecomment%idx%.}
{.if| %user% |{: {.append| %folder-resource%\hfs.comments.txt |{.filename|%item-resource%.}={.postvar|filecomment%idx%.}<br>uploaded by %user%
/append.} :}/if.}




By considering closer hslib.pas, I noticed that we could take advantage of fileupload items.

As you noticed it above it is possible to use additional fields from the form of the section [upload-file],
but all fields 'fileupload%idx%' are not accessible with {.postvar|fileupload%idx%.} because no information is returned.
With the next code the macro can return the distant filename:

Quote
   procedure handleLeftData(i:integer);
    begin
    // the data processed below is related to the previous file
    post.data:=chop(i, length(post.boundary), buffer);
    // if data was found, we must trim the CRLF between data and boundary
    setlength(post.data, length(post.data)-2);
    // we expect this data to have a filename, otherwise it is just discarded
    if post.filename > '' then
      begin
      tryNotify(HE_POST_MORE_FILE);
      post.data:=post.filename;
      tryNotify(HE_POST_VAR);
      end
   else if post.varname > '' then
      tryNotify(HE_POST_VAR);
    FbytesPostedLastItem:=bytesPosted-length(buffer)-lastPostItemPos-length(post.boundary)-2;
    if post.filename > '' then
      tryNotify(HE_POST_END_FILE);
    end; // handleLeftData

Quote
[upload-file]
<input name='fileupload%idx%' size='70' type='file'><input name='filecomment%idx%' size='20' type='text'><br>

[upload-success]
<li><a href='%item-url%'>%item-name%</a>: <b>{.!OK.}</b> --- %item-size%  ({.!Speed.} %speed% KB/s) comment  = {.postvar|filecomment%idx%.}
{.if| %user% |{: {.append| %folder-resource%\hfs.comments.txt |{.filename|%item-resource%.}={.postvar|filecomment%idx%.}<br>'{.postvar|fileupload%idx%.}' uploaded by %user%
/append.} :}/if.}

It has maybe no utility at the moment, but rather than to have that an empty string, the name of the file in the corresponding field is stored in the variable fileupload1 ,2 ,3, ....
Title: Re: only one line to add a user comment on file upload
Post by: rejetto on February 20, 2010, 07:44:15 PM
1. ok, you will find %idx% next version

2. i'm not sure i got what you are doing.
you are trying to retrieve the value of post.filename, but it's already in %filename%