Example #1
0
  /**
   * Helper to create the proposed directory name for the page base on the course node short title.
   * The title is normalized, converted to ASCII and all strange characters are removed. Users
   * should recognize the proposed directory name in most cases. As a fallback, the course node ID
   * is used in case everything is stripped. If a proposed name already exists, the directory will
   * get a number appended. <br>
   * The file name will always be "index.html" <br>
   * The end result will look something like this: <br>
   *
   * <pre>/single_page3/index.html</pre>
   *
   * @param courseNode
   * @param courseFolderBaseContainer
   * @return The proposed relative file path
   */
  public static String createUniqueRelFilePathFromShortTitle(
      CourseNode courseNode, VFSContainer courseFolderBaseContainer) {
    // Normalize course node title to something URL and filesystem safe
    // containing only ASCII characters
    String safePath =
        FileUtils.normalizeFilename(courseNode.getShortTitle().toLowerCase())
            .trim()
            .replaceAll("[^\\p{ASCII}]", "") // delete non-ASCII
            .replaceAll("[\\p{Punct}]+", "_") // replace punctuation characters
            .replaceAll("\\s+", "_") // replace whitespace
            .replaceAll("_+", "_") // multiple replacement strings
            .replaceAll("^_", "") // delete replacement string at beginning
            .replaceAll("_$", "") // delete replacement string at end
            .toLowerCase(Locale.ENGLISH);

    // If string is empty, use node ID instead
    if (!StringHelper.containsNonWhitespace(safePath)) {
      safePath = courseNode.getIdent();
    }

    // Check if directory already exists, if yes, append number at end of
    // directory
    VFSItem direxists = courseFolderBaseContainer.resolve(safePath);
    int i = 1;
    while (direxists != null && i < 100) {
      i++;
      safePath = FileUtils.appendNumberAtTheEndOfFilename(safePath, i);
      direxists = courseFolderBaseContainer.resolve(safePath);
    }

    return "/" + safePath + "/" + safePath + ".html";
  }
Example #2
0
 private void cleanupUnzip() {
   try {
     if (unzipped != null && unzipped.exists()) {
       FileUtils.deleteDirsAndFiles(unzipped, true, true);
     }
   } catch (Exception e) {
     log.error("Cannot cleanup unzipped datas after bulk assessment", e);
   }
 }
Example #3
0
 protected void removeDevToolTests() {
   Locale testLocale = i18nMgr.getLocaleOrDefault("de");
   // cleanup devtools source/target files
   // 1) source files
   File baseDir = I18nModule.getPropertyFilesBaseDir(testLocale, testSourceBundle);
   File testSFile = i18nMgr.getPropertiesFile(testLocale, testSourceBundle, baseDir);
   File sourcePath = testSFile.getParentFile().getParentFile();
   FileUtils.deleteDirsAndFiles(sourcePath, true, true);
   // 2) target files
   baseDir = I18nModule.getPropertyFilesBaseDir(testLocale, testTargetBundle);
   File testTFile = i18nMgr.getPropertiesFile(testLocale, testTargetBundle, baseDir);
   File targetPath = testTFile.getParentFile().getParentFile();
   FileUtils.deleteDirsAndFiles(targetPath, true, true);
   // 3) move target files
   baseDir = I18nModule.getPropertyFilesBaseDir(testLocale, testMoveTargetBundle);
   File testMFile = i18nMgr.getPropertiesFile(testLocale, testMoveTargetBundle, baseDir);
   File movePath = testMFile.getParentFile().getParentFile().getParentFile();
   FileUtils.deleteDirsAndFiles(movePath, true, true);
 }
Example #4
0
 /**
  * Returns a similar but non existing file name in root based on the given name.
  *
  * @param root
  * @param name
  * @return A non existing name based on the given name in the root directory
  */
 public static String similarButNonExistingName(VFSContainer root, String name) {
   VFSItem existingItem = null;
   String newName = name;
   existingItem = root.resolve(newName);
   int i = 1;
   while (existingItem != null) {
     newName = FileUtils.appendNumberAtTheEndOfFilename(name, i++);
     existingItem = root.resolve(newName);
   }
   return newName;
 }
Example #5
0
 private void cleanup() {
   if (StringHelper.containsNonWhitespace(datas.getDataBackupFile())) {
     OlatRootFileImpl backupFile = new OlatRootFileImpl(datas.getDataBackupFile(), null);
     if (backupFile.exists()) {
       File dir = backupFile.getBasefile().getParentFile();
       if (dir != null && dir.exists()) {
         FileUtils.deleteDirsAndFiles(dir, true, true);
       }
     }
   }
   cleanupUnzip();
 }
 @Override
 public boolean isThumbnailPossible(VFSLeaf file) {
   String extension = FileUtils.getFileSuffix(file.getName());
   if (StringHelper.containsNonWhitespace(extension)) {
     for (ThumbnailSPI thumbnailSPI : thumbnailSPIes) {
       if (thumbnailSPI.getExtensions().contains(extension)) {
         return true;
       }
     }
   }
   return false;
 }
Example #7
0
 /**
  * @param container
  * @param filename
  * @return
  */
 public static String rename(VFSContainer container, String filename) {
   String newName = filename;
   VFSItem newFile = container.resolve(newName);
   for (int count = 0; newFile != null && count < 999; ) {
     count++;
     newName = FileUtils.appendNumberAtTheEndOfFilename(filename, count);
     newFile = container.resolve(newName);
   }
   if (newFile == null) {
     return newName;
   }
   return null;
 }
 /**
  * @param source
  * @param encoding
  * @return the inpustream in form of a string
  */
 public static String load(InputStream source, String encoding) {
   String htmltext = null;
   try {
     ByteArrayOutputStream bas = new ByteArrayOutputStream();
     boolean success = FileUtils.copy(source, bas);
     source.close();
     bas.close();
     if (!success) throw new RuntimeException("could not copy inputstream to ram");
     htmltext = bas.toString(StringHelper.check4xMacRoman(encoding));
   } catch (IOException e) {
     throw new OLATRuntimeException(FileUtils.class, "could not load from inputstream", e);
   }
   return htmltext;
 }
Example #9
0
 /**
  * Recursively traverse the container and sum up all leafs' sizes.
  *
  * @param container
  * @return
  */
 public static long getUsageKB(VFSItem vfsItem) {
   if (vfsItem instanceof VFSContainer) {
     // VFSContainer
     if (vfsItem instanceof LocalFolderImpl)
       return FileUtils.getDirSize(((LocalFolderImpl) vfsItem).getBasefile()) / 1024;
     long usageKB = 0;
     List<VFSItem> children = ((VFSContainer) vfsItem).getItems();
     for (VFSItem child : children) {
       usageKB += getUsageKB(child);
     }
     return usageKB;
   } else {
     // VFSLeaf
     return ((VFSLeaf) vfsItem).getSize() / 1024;
   }
 }
 @Override
 public FinalSize generateThumbnail(
     VFSLeaf file, VFSLeaf thumbnailFile, int maxWidth, int maxHeight)
     throws CannotGenerateThumbnailException {
   String extension = FileUtils.getFileSuffix(file.getName()).toLowerCase();
   for (ThumbnailSPI thumbnailSPI : thumbnailSPIes) {
     if (thumbnailSPI.getExtensions().contains(extension)) {
       FinalSize finalSize =
           thumbnailSPI.generateThumbnail(file, thumbnailFile, maxWidth, maxHeight);
       if (finalSize != null) {
         return finalSize;
       } // else, try to find an other SPI which can thumbnailed this file
     }
   }
   return null;
 }
Example #11
0
 @After
 public void tearDown() throws Exception {
   String testNewBundle = "org.olat.core.util.i18n.junittestdata.new";
   Locale testLocale = i18nMgr.getLocaleOrDefault("de");
   File baseDir = I18nModule.getPropertyFilesBaseDir(testLocale, testNewBundle);
   // only delete files when basedir available
   if (baseDir == null) return;
   File testFile = i18nMgr.getPropertiesFile(testLocale, testNewBundle, baseDir);
   // delete not only new/_i18n/LocalStrings_de.properties, delete also _i18n and new itself
   if (testFile.getParentFile().getParentFile().exists()) {
     FileUtils.deleteDirsAndFiles(testFile.getParentFile().getParentFile(), true, true);
   }
   // don't do inline translation markup
   i18nMgr.setMarkLocalizedStringsEnabled(null, false);
   // cleanup dev tools test case files
   removeDevToolTests();
 }
Example #12
0
  /**
   * Copy the content of the source container to the target container.
   *
   * @param source
   * @param target
   * @return
   */
  public static boolean copyContent(VFSContainer source, VFSContainer target) {
    if (!source.exists()) {
      return false;
    }
    if (isSelfOrParent(source, target)) {
      return false;
    }

    if (source instanceof NamedContainerImpl) {
      source = ((NamedContainerImpl) source).getDelegate();
    }
    if (target instanceof NamedContainerImpl) {
      target = ((NamedContainerImpl) target).getDelegate();
    }

    if (source instanceof LocalImpl && target instanceof LocalImpl) {
      LocalImpl localSource = (LocalImpl) source;
      LocalImpl localTarget = (LocalImpl) target;
      File localSourceFile = localSource.getBasefile();
      File localTargetFile = localTarget.getBasefile();
      return FileUtils.copyDirContentsToDir(localSourceFile, localTargetFile, false, "VFScopyDir");
    }
    return false;
  }