Package org.damour.base.client.objects

Examples of org.damour.base.client.objects.FileUploadStatus


      fileItems = getFileItems(request, owner);
    } catch (Throwable t) {
      Logger.log(t);
      return;
    }
    final FileUploadStatus status = BaseService.fileUploadStatusMap.get(owner);
    status.setStatus(FileUploadStatus.CREATING_FILE);

    Transaction tx = session.beginTransaction();

    try {
      for (final FileItem item : fileItems) {

        File fileObject = null;
        if (item.getContentType().contains("image")) {
          fileObject = new Photo();
        } else {
          fileObject = new File();
        }
        fileObject.setOwner(owner);

        String parentFolderId = request.getParameter("parentFolder");
        Folder parentFolder = null;
        if (parentFolderId != null && !"".equals(parentFolderId)) {
          parentFolder = (Folder) session.load(Folder.class, new Long(parentFolderId));
          if (!SecurityHelper.doesUserHavePermission(session, owner, parentFolder, PERM.WRITE)) {
            Cookie cookie = new Cookie(item.getFieldName(), "");
            cookie.setMaxAge(0);
            cookie.setPath("/");
            response.addCookie(cookie);
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            response.flushBuffer();
            Logger.log("Unauthorized upload requested: " + request.getRemoteAddr());
            return;
          }
        }
        fileObject.setParent(parentFolder);

        fileObject.setContentType(item.getContentType());
        String name = item.getName();
        if (name.lastIndexOf("/") >= 0) {
          name = name.substring(name.lastIndexOf("/") + 1);
        }
        if (name.lastIndexOf("\\") >= 0) {
          name = name.substring(name.lastIndexOf("\\") + 1);
        }

        fileObject.setName(name);
        fileObject.setDescription(name);
        session.save(fileObject);

        status.setStatus(FileUploadStatus.WRITING_FILE);
        java.io.File outputPath = new java.io.File(java.io.File.separatorChar + "tmp" + java.io.File.separatorChar + BaseSystem.getDomainName(request));
        java.io.File outputFile = new java.io.File(outputPath, fileObject.getId() + "_" + fileObject.getName());
        outputPath.mkdirs();
        item.write(outputFile);
        tx = session.beginTransaction();
        fileObject.setNameOnDisk(fileObject.getId() + "_" + fileObject.getName());
        fileObject.setSize(outputFile.length());
        session.save(fileObject);
        tx.commit();
        Logger.log("Wrote to file: " + outputFile.getCanonicalPath());

        status.setStatus(FileUploadStatus.WRITING_DATABASE);
        saveFileFromStream(fileObject, new FileInputStream(outputFile));

        try {

          // if we are uploading a photo, create slideshow (sane size) image and thumbnail
          if (fileObject instanceof Photo) {
            // convert file to png for faster inclusion in thumbnails
            BufferedImage bi = javax.imageio.ImageIO.read(outputFile);
            ByteArrayOutputStream pngOut = new ByteArrayOutputStream();
            ImageIO.write(bi, "png", pngOut);

            tx = session.beginTransaction();
            Photo photo = (Photo) fileObject;
            photo.setWidth(bi.getWidth());
            photo.setHeight(bi.getHeight());

            PhotoThumbnail thumbFile = new PhotoThumbnail();
            thumbFile.setHidden(true);
            thumbFile.setOwner(owner);
            thumbFile.setParent(parentFolder);
            thumbFile.setName(createFileName("", name, "_thumb"));
            thumbFile.setDescription("Thumbnail for " + name);
            session.save(thumbFile);

            PhotoThumbnail previewFile = new PhotoThumbnail();
            previewFile.setHidden(true);
            previewFile.setOwner(owner);
            previewFile.setParent(parentFolder);
            previewFile.setName(createFileName("", name, "_preview"));
            previewFile.setDescription("Preview image for " + name);
            session.save(previewFile);
           
            PhotoThumbnail slideFile = new PhotoThumbnail();
            slideFile.setHidden(true);
            slideFile.setOwner(owner);
            slideFile.setParent(parentFolder);
            slideFile.setName(createFileName("", name, "_slideshow"));
            slideFile.setDescription("Slideshow image for " + name);
            session.save(slideFile);

           
            photo.setThumbnailImage(thumbFile);
            photo.setSlideshowImage(slideFile);
            photo.setPreviewImage(previewFile);

           
            ByteArrayInputStream pngIn = new ByteArrayInputStream(pngOut.toByteArray());

            ByteArrayOutputStream thumbOutStream = new ByteArrayOutputStream();
            BufferedImage thumbbi = ImageIO.read(pngIn);
            BufferedImage convertedThumbImage = createThumbnail(thumbbi, 128, 128, true);
            thumbFile.setWidth(convertedThumbImage.getWidth());
            thumbFile.setHeight(convertedThumbImage.getHeight());

            ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
            // instantiate an ImageWriteParam object with default compression options
            ImageWriteParam iwp = writer.getDefaultWriteParam();

            iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            iwp.setCompressionQuality(0.75f);
            writer.setOutput(new MemoryCacheImageOutputStream(thumbOutStream));
            IIOImage thumbimage = new IIOImage(convertedThumbImage, null, null);
            writer.write(null, thumbimage, iwp);

            pngIn.reset();
            ByteArrayOutputStream slideOutStream = new ByteArrayOutputStream();
            BufferedImage slidebi = ImageIO.read(pngIn);
            BufferedImage convertedSlideImage = createThumbnail(slidebi, 640, 400, true);
            slideFile.setWidth(convertedSlideImage.getWidth());
            slideFile.setHeight(convertedSlideImage.getHeight());
            writer.setOutput(new MemoryCacheImageOutputStream(slideOutStream));
            IIOImage slideimage = new IIOImage(convertedSlideImage, null, null);
            writer.write(null, slideimage, iwp);

            pngIn.reset();
            ByteArrayOutputStream previewOutStream = new ByteArrayOutputStream();
            BufferedImage previewbi = ImageIO.read(pngIn);
            BufferedImage convertedPreviewImage = createThumbnail(previewbi, 320, 200, true);
            previewFile.setWidth(convertedPreviewImage.getWidth());
            previewFile.setHeight(convertedPreviewImage.getHeight());
            writer.setOutput(new MemoryCacheImageOutputStream(previewOutStream));
            IIOImage previewimage = new IIOImage(convertedPreviewImage, null, null);
            writer.write(null, previewimage, iwp);           
           
            thumbFile.setContentType("image/jpeg");
            slideFile.setContentType("image/jpeg");
            previewFile.setContentType("image/jpeg");
            thumbFile.setSize(thumbOutStream.size());
            slideFile.setSize(slideOutStream.size());
            previewFile.setSize(previewOutStream.size());
            session.save(thumbFile);
            session.save(slideFile);
            session.save(previewFile);
            tx.commit();

            tx = session.beginTransaction();
            thumbFile.setNameOnDisk(createFileName(thumbFile.getId().toString() + "_", name, "_thumb"));
            slideFile.setNameOnDisk(createFileName(slideFile.getId().toString() + "_", name, "_slide"));
            previewFile.setNameOnDisk(createFileName(previewFile.getId().toString() + "_", name, "_preview"));
            tx.commit();

            saveFileFromStream(thumbFile, new ByteArrayInputStream(thumbOutStream.toByteArray()));
            saveFileFromStream(slideFile, new ByteArrayInputStream(slideOutStream.toByteArray()));
            saveFileFromStream(previewFile, new ByteArrayInputStream(previewOutStream.toByteArray()));
          }

          status.setStatus(FileUploadStatus.FINISHED);
          Logger.log("Wrote to database: " + fileObject.getName());
        } catch (Throwable t) {
          Logger.log(t);
        }
        Cookie cookie = new Cookie(item.getFieldName(), fileObject.getId().toString());
View Full Code Here


    repository.mkdirs();

    DiskFileItemFactory fileFactory = new DiskFileItemFactory(sizeThreshold, repository);
    ServletFileUpload fu = new ServletFileUpload(fileFactory);

    final FileUploadStatus status = new FileUploadStatus();
    status.setStatus(FileUploadStatus.UPLOADING);
    BaseService.fileUploadStatusMap.put(user, status);

    // If file size exceeds, a FileUploadException will be thrown
    fu.setSizeMax(268435456);
    fu.setProgressListener(new ProgressListener() {
      public void update(long bytesRead, long contentLength, int item) {
        status.setItem(item);
        status.setBytesRead(bytesRead);
        status.setContentLength(contentLength);
        status.setStatus(FileUploadStatus.UPLOADING);
        try {
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
View Full Code Here

    User authUser = getAuthenticatedUser(session.get());
    if (authUser == null) {
      throw new SimpleMessageException("User is not authenticated.");
    }
    try {
      FileUploadStatus status = fileUploadStatusMap.get(authUser);
      if (status == null) {
        throw new SimpleMessageException("No stats currently available.");
      }
      return status;
    } catch (Throwable t) {
View Full Code Here

TOP

Related Classes of org.damour.base.client.objects.FileUploadStatus

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.