You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Taha Hafeez <ta...@gmail.com> on 2011/03/29 10:21:19 UTC

Tapestry FileUploader Integration

Dear all,

I had a requirement for a FileUploader and found this
https://github.com/valums/file-uploader

Integration with Tapestry5 was as usual very easy... Am sharing the
code, you might find it useful

package somepackage.components;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.tapestry5.BindingConstants;
import org.apache.tapestry5.ClientElement;
import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.Link;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.SupportsInformalParameters;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONLiteral;
import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.RequestGlobals;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;

import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

@SupportsInformalParameters
@Import(library = "fileuploader/fileuploader.js", stylesheet =
"fileuploader/fileuploader.css")
public class FileUploader implements ClientElement {

   @Parameter(value = "prop:componentResources.id", defaultPrefix =
BindingConstants.LITERAL)
   private String clientId;

   @Inject
   private JavaScriptSupport javaScriptSupport;

   @Inject
   private ComponentResources resources;

   @Parameter(required = true, allowNull = false)
   private List<String> filenames;

   @Parameter
   private String tempDir;

   @Parameter
   private String clientFileName;

   @SuppressWarnings("unused")
   @Parameter
   private boolean uploaded;

   @Parameter(value = "2000000", defaultPrefix = BindingConstants.LITERAL)
   private int maxSize;

   @Parameter(value = "0", defaultPrefix = BindingConstants.LITERAL)
   private int minSize;

   @Parameter(value = "1", defaultPrefix = BindingConstants.LITERAL)
   private int maxFiles;

   @Inject
   private Request request;

   @Inject
   private Messages messages;

   @Inject
   private RequestGlobals globals;

   private String assignedClientId;

   @Parameter(value = "tmp", allowNull = false, defaultPrefix =
BindingConstants.LITERAL)
   private String prefix;

   public void beginRender() {
      assignedClientId = javaScriptSupport.allocateClientId(clientId);
   }

   public void afterRender(final MarkupWriter writer) {
      writer.element("div", "id", getClientId());
      writer.end();

      final Link link = resources.createEventLink("upload");
      JSONObject spec = new JSONObject();
      spec.put("element", new JSONLiteral("document.getElementById('"
            + getClientId() + "')"));
      spec.put("action", link.toAbsoluteURI());
      spec.put("sizeLimit", maxSize);
      spec.put("minSizeLimit", minSize);
      for (String informalParameter : resources.getInformalParameterNames())
{
         spec.put(informalParameter, resources.getInformalParameter(
               informalParameter, String.class));
      }
      javaScriptSupport.addScript("new qq.FileUploader(%s);", spec);
   }

   Object onUpload() {
      uploaded = false;
      String errorMessage = null;
      clientFileName = request.getParameter("qqfile");
      String suffix = ".tmp";
      if(clientFileName.lastIndexOf('.') != -1){
         suffix = clientFileName.substring(0,
clientFileName.lastIndexOf('.') - 1);
      }

      if(maxFiles <= filenames.size()){
         JSONObject spec = new JSONObject();
         spec.put("success", false).put("error",
messages.format("fileuploader.maxfiles", maxFiles));
         return spec;
      }

      try {
         InputStream in = globals.getHTTPServletRequest().getInputStream();
         ByteOutputStream bout = new ByteOutputStream();
         byte[] buf = new byte[8096];
         while (true) {
            int bytesRead = in.read(buf);
            if (bytesRead == -1) {
               break;
            }
            bout.write(buf, 0, bytesRead);
         }
         bout.close();

         if (bout.getCount() < minSize) {
            errorMessage = messages.get("fileupload.minsize");
         } else if (bout.getCount() > maxSize) {
            errorMessage = messages.get("fileupload.maxsize");
         } else {

            FileOutputStream fout = null;
            try {
               File dir = null;
               if(tempDir != null){
                  dir = new File(tempDir);
                  dir.mkdir();
               }
               File tempFile = File.createTempFile(prefix, suffix,  dir);
               fout = new FileOutputStream(tempFile);
               fout.write(bout.getBytes(), 0, bout.getCount());
               fout.close();
               uploaded = true;
               filenames.add(tempFile.getAbsolutePath());
            } catch (FileNotFoundException e) {
               errorMessage = e.getMessage();
            } catch (IOException e) {
               errorMessage = e.getMessage();
            } finally {
               if (fout != null) {
                  fout.close();
               }
            }
         }
      } catch (IOException e) {
         errorMessage = messages.get("fileupload.writeerror");
      }

      JSONObject result = new JSONObject();
      if (errorMessage != null) {
         result.put("success", false);
         result.put("error", errorMessage);
      } else {
         result.put("success", true);
      }

      return result;
   }

   public String getClientId() {
      return assignedClientId;
   }
}


regards
Taha

Re: Tapestry FileUploader Integration

Posted by Taha Hafeez <ta...@gmail.com>.
Hi Angelo

I know I should be more organized but am working on an office project with a
tough deadline and also on my PhD, so it is very difficult to manage. Will
surely do it whenever I get some spare time and I have a lot to contribute
especially from the crud library I have created for my office projects.

regards
Taha



On Tue, Mar 29, 2011 at 8:19 PM, Angelo C. <an...@gmail.com> wrote:

> very nice component, also your modalbox, how about putting all those into
> github? this will be easier for us to follow.
>
> --
> View this message in context:
> http://tapestry.1045711.n5.nabble.com/Tapestry-FileUploader-Integration-tp4268987p4269518.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Tapestry FileUploader Integration

Posted by "Angelo C." <an...@gmail.com>.
very nice component, also your modalbox, how about putting all those into
github? this will be easier for us to follow.

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Tapestry-FileUploader-Integration-tp4268987p4269518.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org