Exemplo n.º 1
0
  public StringBuilder prepareImages(StringBuilder s) {
    int start = 0;

    // StringBuilder lower_case = new StringBuilder( s.toString().toLowerCase() );
    List<FileAttachment> attached_images = new ArrayList();

    for (Attachment att : attachments) {
      // System.out.println(att.getClass().getName());
      if (att instanceof FileAttachment) {
        FileAttachment fatt = (FileAttachment) att;

        String mime_type = fatt.getMimeTag();

        logger.info(fatt.toString() + " " + mime_type);

        if (mime_type != null && MainWin.is_image_mime_type(mime_type)) {
          attached_images.add(fatt);
        }
      }
    }

    Collections.sort(
        attached_images,
        new Comparator<FileAttachment>() {

          @Override
          public int compare(FileAttachment o1, FileAttachment o2) {
            if (o1.getSubDir() != null && o2.getSubDir() != null) {
              return o1.getSubDir().compareTo(o2.getSubDir());
            }

            return o1.getFilename().compareTo(o2.getFilename());
          }
        });

    while ((start = findImgTag(s, start)) >= 0) {
      // System.out.println("HERE");

      int end = s.indexOf(">", start);

      if (end < 0) {
        break;
      }

      String res = replace_src(new StringBuilder(s.subSequence(start, end)), attached_images);

      s.replace(start, end, res);

      if (res.length() == 0) start++;
      else start += res.length();
    }

    return s;
  }
Exemplo n.º 2
0
  private String replace_src(StringBuilder s, List<FileAttachment> attached_images) {

    String res[] = s.toString().split("[sS][rR][cC]\\s*=");

    int i = 0;

    StringBuilder ret = new StringBuilder();

    for (String part : res) {
      i++;

      if (i == 1) {
        ret.append(part);
        continue;
      }

      ret.append("src=");

      int start = part.indexOf("\"");
      int end = -1;

      if (start >= 0) end = part.indexOf("\"", start + 1);

      String src = part;
      String rest = "";

      if (start >= 0 && end > 0) {
        src = part.substring(start + 1, end);

        if (end > 0) rest = part.substring(end + 1);
      }

      // String src = StringUtils.strip(new StringBuilder(part), " \t\r\n\"");

      String imgsrc = src;

      if (!src.toLowerCase().startsWith("http:") && !src.isEmpty()) {
        logger.info("HERE");
        imgsrc = "file:/" + extra + base_dir + "/" + src;
        File file = new File(extra + base_dir + "/" + src);

        if (!file.exists() && !attached_images.isEmpty()) {
          if (src.toLowerCase().startsWith("cid:")) {
            String cid = src.substring(4);

            for (FileAttachment fatt : attached_images) {
              if (fatt.getCid() != null) {
                if (fatt.getCid().equals(cid)) {

                  imgsrc = "file:/" + extra + base_dir + "/" + getFileName(fatt);
                  attached_images.remove(fatt);
                  break;
                }
              }
            }

          } else {

            imgsrc = "file:/" + extra + base_dir + "/" + getFileName(attached_images.remove(0));
          }
        }
      }

      logger.info("image: " + src);

      if (imgsrc != null) {
        ret.append("\"");
        ret.append(imgsrc);
        ret.append("\"");
        ret.append(rest);
      }
    }

    return ret.toString();
  }
Exemplo n.º 3
0
  static String getFileName(FileAttachment fatt) {
    if (fatt.getFilename() == null || fatt.getFilename().isEmpty()) return fatt.getLongFilename();

    return fatt.getFilename();
  }
Exemplo n.º 4
0
  /** Import MSG file as MailNode. */
  private String importMsg(String path, InputStream is)
      throws MessagingException, PathNotFoundException, ItemExistsException, VirusDetectedException,
          AccessDeniedException, RepositoryException, DatabaseException, UserQuotaExceededException,
          UnsupportedMimeTypeException, FileSizeExceededException, ExtensionException,
          AutomationException, IOException {
    log.debug("importMsg({}, {})", path, is);
    String errorMsg = null;

    try {
      // Convert file
      MsgParser msgp = new MsgParser();
      Message msg = msgp.parseMsg(is);
      Mail mail = MailUtils.messageToMail(msg);

      // Create phantom path. In this case we don't have the IMAP message ID, son create a random
      // one.
      mail.setPath(
          path + "/" + UUID.randomUUID().toString() + "-" + PathUtils.escape(mail.getSubject()));

      // Import files
      OKMMail.getInstance().create(null, mail);

      for (Attachment att : msg.getAttachments()) {
        if (att instanceof FileAttachment) {
          FileAttachment fileAtt = (FileAttachment) att;
          log.debug("Importing attachment: {}", fileAtt.getFilename());

          String fileName = fileAtt.getFilename();
          String fileExtension = fileAtt.getExtension();
          String testName = fileName + "." + fileExtension;

          // Test if already exists a document with the same name in the mail
          for (int j = 1;
              OKMRepository.getInstance().hasNode(null, mail.getPath() + "/" + testName);
              j++) {
            // log.debug("Trying with: {}", testName);
            testName = fileName + " (" + j + ")." + fileExtension;
          }

          Document attachment = new Document();
          String mimeType = MimeTypeConfig.mimeTypes.getContentType(testName.toLowerCase());
          attachment.setMimeType(mimeType);
          attachment.setPath(mail.getPath() + "/" + testName);
          ByteArrayInputStream bais = new ByteArrayInputStream(fileAtt.getData());

          if (Config.REPOSITORY_NATIVE) {
            new DbDocumentModule()
                .create(null, attachment, bais, fileAtt.getSize(), PrincipalUtils.getUser());
          } else {
            new JcrDocumentModule().create(null, attachment, bais, PrincipalUtils.getUser());
          }

          IOUtils.closeQuietly(bais);
        }
      }
    } catch (IOException e) {
      log.error("Error importing msg", e);
      throw e;
    } finally {
      IOUtils.closeQuietly(is);
    }

    log.debug("importMsg: {}", errorMsg);
    return errorMsg;
  }