Exemplo n.º 1
0
 /**
  * Extract the mime-type from the file name.
  *
  * @param fileName the name of the file.
  * @return the mime-type as a String.
  */
 public static String getMimeType(final String fileName) {
   String mimeType = null;
   final String fileExtension = FileRepositoryManager.getFileExtension(fileName).toLowerCase();
   try {
     if (MIME_TYPES_EXTENSIONS != null) {
       mimeType = MIME_TYPES_EXTENSIONS.getString(fileExtension);
     }
   } catch (final MissingResourceException e) {
     SilverTrace.warn(
         "attachment",
         "AttachmentController",
         "attachment.MSG_MISSING_MIME_TYPES_PROPERTIES",
         null,
         e);
   }
   if (mimeType == null) {
     mimeType = MIME_TYPES.getContentType(fileName);
   }
   if (ARCHIVE_MIME_TYPE.equalsIgnoreCase(mimeType)
       || SHORT_ARCHIVE_MIME_TYPE.equalsIgnoreCase(mimeType)) {
     if (JAR_EXTENSION.equalsIgnoreCase(fileExtension)
         || WAR_EXTENSION.equalsIgnoreCase(fileExtension)
         || EAR_EXTENSION.equalsIgnoreCase(fileExtension)) {
       mimeType = JAVA_ARCHIVE_MIME_TYPE;
     } else if ("3D".equalsIgnoreCase(fileExtension)) {
       mimeType = SPINFIRE_MIME_TYPE;
     }
   }
   if (mimeType == null) {
     mimeType = DEFAULT_MIME_TYPE;
   }
   return mimeType;
 }
 public String getDocumentIcon() {
   String icon = "";
   if (getPhysicalName().lastIndexOf('.') >= 0) {
     String fileType = FileRepositoryManager.getFileExtension(getPhysicalName());
     icon = FileRepositoryManager.getFileIcon(fileType);
   } else {
     icon = FileRepositoryManager.getFileIcon("");
   }
   return icon;
 }
  /**
   * Methode de recuperation des attachements et de copie des fichiers dans le dossier d'exportation
   *
   * @param pk - PrimaryKey de l'obijet dont on veut les attachments?
   * @param exportPath - Repertoire dans lequel copier les fichiers
   * @param relativeExportPath chemin relatif du fichier copie
   * @param extensionFilter
   * @return une liste des attachmentDetail trouves
   */
  public List<AttachmentDetail> getAttachments(
      WAPrimaryKey pk, String exportPath, String relativeExportPath, String extensionFilter) {

    // Recuperation des attachments
    Collection<SimpleDocument> listAttachment =
        AttachmentServiceFactory.getAttachmentService().listDocumentsByForeignKey(pk, null);
    List<AttachmentDetail> listToReturn = new ArrayList<AttachmentDetail>(listAttachment.size());
    if (!listAttachment.isEmpty()) {
      // Pour chaque attachment trouve, on copie le fichier dans le dossier
      // d'exportation
      for (SimpleDocument attachment : listAttachment) {
        if (attachment.getDocumentType() != DocumentType.attachment) {
          // ce n est pas un fichier joint mais un fichier appartenant surement
          // au wysiwyg si le context
          // est different de images et ce quelque soit le type du fichier
          continue; // on ne copie pas le fichier
        }

        if (!attachment.isDownloadAllowedForRolesFrom(user)) {
          // The user is not allowed to download this document. No error is thrown but the
          // document is not exported.
          continue;
        }

        if (extensionFilter == null
            || FileRepositoryManager.getFileExtension(attachment.getFilename())
                .equalsIgnoreCase(extensionFilter)) {
          copyAttachment(attachment, exportPath);
          String physicalName =
              relativeExportPath
                  + File.separator
                  + FileServerUtils.replaceAccentChars(attachment.getFilename());
          AttachmentDetail attachDetail =
              new AttachmentDetail(
                  new AttachmentPK(attachment.getId(), attachment.getInstanceId()),
                  physicalName,
                  attachment.getFilename(),
                  attachment.getDescription(),
                  attachment.getContentType(),
                  attachment.getSize(),
                  attachment.getDocumentType().toString(),
                  attachment.getCreated(),
                  new ForeignPK(attachment.getForeignId(), attachment.getInstanceId()));
          listToReturn.add(attachDetail);
        }
      }
    }

    return listToReturn;
  }
  /**
   * Uploads the file containing the video and that is identified by the specified field name.
   *
   * @param items the items of the form. One of them is containg the video file.
   * @param itemKey the key of the item containing the video.
   * @param pageContext the context of the page displaying the form.
   * @throws Exception if an error occurs while uploading the video file.
   * @return the identifier of the uploaded attached video file.
   */
  private String uploadVideoFile(
      final List<FileItem> items, final String itemKey, final PagesContext pageContext)
      throws Exception {
    String attachmentId = "";

    FileItem item = FileUploadUtil.getFile(items, itemKey);
    if (!item.isFormField()) {
      String componentId = pageContext.getComponentId();
      String userId = pageContext.getUserId();
      String objectId = pageContext.getObjectId();
      String logicalName = item.getName();
      long size = item.getSize();
      if (StringUtil.isDefined(logicalName)) {
        if (!FileUtil.isWindows()) {
          logicalName = logicalName.replace('\\', File.separatorChar);
          SilverTrace.info(
              "form",
              "VideoFieldDisplayer.uploadVideoFile",
              "root.MSG_GEN_PARAM_VALUE",
              "fullFileName on Unix = " + logicalName);
        }

        logicalName =
            logicalName.substring(
                logicalName.lastIndexOf(File.separator) + 1, logicalName.length());
        String type = FileRepositoryManager.getFileExtension(logicalName);
        String mimeType = item.getContentType();
        String physicalName = Long.toString(System.currentTimeMillis()) + "." + type;
        File dir = getVideoPath(componentId, physicalName);
        item.write(dir);
        AttachmentDetail attachmentDetail =
            createAttachmentDetail(
                objectId,
                componentId,
                physicalName,
                logicalName,
                mimeType,
                size,
                VideoFieldDisplayer.CONTEXT_FORM_VIDEO,
                userId);
        attachmentDetail = AttachmentController.createAttachment(attachmentDetail, true);
        attachmentId = attachmentDetail.getPK().getId();
      }
    }

    return attachmentId;
  }
  /**
   * Retrieve Publication Templates
   *
   * @param onlyVisibles only visible templates boolean
   * @return only visible PublicationTemplates if onlyVisible is true, all the publication templates
   *     else if
   * @throws PublicationTemplateException
   */
  public List<PublicationTemplate> getPublicationTemplates(boolean onlyVisibles)
      throws PublicationTemplateException {
    List<PublicationTemplate> publicationTemplates = new ArrayList<PublicationTemplate>();
    Collection<File> templateNames;
    try {
      templateNames = FileFolderManager.getAllFile(templateDir);
    } catch (UtilException e1) {
      throw new PublicationTemplateException(
          "PublicationTemplateManager.getPublicationTemplates",
          "form.EX_ERR_CASTOR_LOAD_PUBLICATION_TEMPLATES",
          e1);
    }
    for (File templateFile : templateNames) {
      String fileName = templateFile.getName();
      try {
        String extension = FileRepositoryManager.getFileExtension(fileName);
        if ("xml".equalsIgnoreCase(extension)) {
          PublicationTemplate template =
              loadPublicationTemplate(
                  fileName.substring(fileName.lastIndexOf(File.separator) + 1, fileName.length()));
          if (onlyVisibles) {
            if (template.isVisible()) {
              publicationTemplates.add(template);
            }
          } else {
            publicationTemplates.add(template);
          }
        }
      } catch (Exception e) {
        SilverTrace.error(
            "form",
            "PublicationTemplateManager.getPublicationTemplates",
            "form.EX_ERR_CASTOR_LOAD_PUBLICATION_TEMPLATE",
            "fileName = " + fileName);
      }
    }

    return publicationTemplates;
  }