Package net.sourceforge.pebble.domain

Examples of net.sourceforge.pebble.domain.FileManager


        }
      } else {
        filename = "export.zip";
      }

      FileManager fileManager = new FileManager(blog, type);
      List files = fileManager.getFiles(path, true);

      return new ZipView(files, filename);
    } catch (IllegalFileAccessException e) {
      return new ForbiddenView();
    }
View Full Code Here


  /**
   * Tests that files can be accessed.
   */
  public void testViewFiles() throws Exception {
    // create some files and directories
    FileManager fileManager = new FileManager(blog, FileMetaData.BLOG_FILE);
    fileManager.createDirectory("/", "a");
    fileManager.createDirectory("/", "z");
    fileManager.saveFile("/", "y.txt", "Some content");
    fileManager.saveFile("/", "b.txt", "Some content");

    request.setParameter("path", "/");
    request.setParameter("type", FileMetaData.BLOG_FILE);

    View view = action.process(request, response);

    // check file information available and the right view is returned
    assertEquals(FileMetaData.BLOG_FILE, action.getModel().get("type"));
    FileMetaData fileMetaData = (FileMetaData)action.getModel().get("directory");
    assertEquals("/", fileMetaData.getAbsolutePath());
    assertTrue(fileMetaData.isDirectory());
    assertEquals("uploadFileToBlog.secureaction", action.getModel().get("uploadAction"));
    assertTrue(view instanceof FilesView);

    List files = (List)action.getModel().get("files");
    assertEquals(4, files.size());

    // the files should be in this order
    // - a, z, b.txt, y.txt (directories followed by files, both alphabetically)
    FileMetaData file = (FileMetaData)files.get(0);
    assertEquals("a", file.getName());
    assertEquals("/", file.getPath());
    assertTrue(file.isDirectory());
    file = (FileMetaData)files.get(1);
    assertEquals("z", file.getName());
    assertEquals("/", file.getPath());
    assertTrue(file.isDirectory());
    file = (FileMetaData)files.get(2);
    assertEquals("b.txt", file.getName());
    assertEquals("/", file.getPath());
    assertFalse(file.isDirectory());
    file = (FileMetaData)files.get(3);
    assertEquals("y.txt", file.getName());
    assertEquals("/", file.getPath());
    assertFalse(file.isDirectory());

    // and clean up
    fileManager.deleteFile("/", "a");
    fileManager.deleteFile("/", "z");
    fileManager.deleteFile("/", "y.txt");
    fileManager.deleteFile("/", "b.txt");
  }
View Full Code Here

  /**
   * Tests that files can be accessed, even when the path isn't specified.
   */
  public void testViewFilesWhenPathNotSpecified() throws Exception {
    // create some files and directories
    FileManager fileManager = new FileManager(blog, FileMetaData.BLOG_FILE);
    fileManager.saveFile("/", "a.txt", "Some content");

    request.setParameter("type", FileMetaData.BLOG_FILE);
    action.process(request, response);
    List files = (List)action.getModel().get("files");
    assertEquals(1, files.size());
    FileMetaData file = (FileMetaData)files.get(0);
    assertEquals("a.txt", file.getName());
    assertEquals("/", file.getPath());
    assertFalse(file.isDirectory());

    // and the same test with a blank path
    request.setParameter("path", "");
    action.process(request, response);
    files = (List)action.getModel().get("files");
    assertEquals(1, files.size());
    file = (FileMetaData)files.get(0);
    assertEquals("a.txt", file.getName());
    assertEquals("/", file.getPath());
    assertFalse(file.isDirectory());

    // and clean up
    fileManager.deleteFile("/", "a.txt");
  }
View Full Code Here

    request.setParameter("fileContent", "Some content.");

    View view = action.process(request, response);

    // check file was saved and the right view is returned
    FileManager fileManager = new FileManager(blog, FileMetaData.BLOG_FILE);
    assertEquals("Some content.", fileManager.loadFile("/", "afile.txt"));
    assertTrue(view instanceof ForwardView);

    // and clean up
    fileManager.deleteFile("/", "afile.txt");
  }
View Full Code Here

    authenticate(blog, username, password);

    Hashtable ht = new Hashtable();

    String name = (String)struct.get(NAME);
    FileManager manager;
    if (name.startsWith("files/")) {
      manager = new FileManager(blog, FileMetaData.BLOG_FILE);
      name = name.substring(name.indexOf("/"));
    } else if (name.startsWith("images/")) {
      manager = new FileManager(blog, FileMetaData.BLOG_IMAGE);
      name = name.substring(name.indexOf("/"));
    } else {
      manager = new FileManager(blog, FileMetaData.BLOG_IMAGE);
      // name is as specified
    }

    log.debug("Saving to " + name);
    try {
      byte bytes[] = (byte[])struct.get(BITS);
      long itemSize = bytes.length/1024; // number of bytes / 1024
      if (FileManager.hasEnoughSpace(blog, itemSize)) {
        FileMetaData file = manager.saveFile(name, bytes);
        ht.put(URL, file.getUrl());
      } else {
        throw new XmlRpcException(0, "You do not have enough free space - please free some space by removing unused files or asking your system administrator to increase your quota from " + PebbleContext.getInstance().getConfiguration().getFileUploadQuota() + " KB.");
      }
    } catch (IOException e) {
View Full Code Here

    String type = getType();
    String path = "";
    String[] filenames = new String[10];

    FileManager fileManager = new FileManager(blog, type);

    try {
      boolean isMultipart = FileUpload.isMultipartContent(request);

      if (isMultipart) {
        DiskFileUpload upload = new DiskFileUpload();
        long sizeInBytes = PebbleContext.getInstance().getConfiguration().getFileUploadSize() * 1024; // convert to bytes
        upload.setSizeMax(sizeInBytes);
        upload.setSizeThreshold((int)sizeInBytes/4);
        upload.setRepositoryPath(System.getProperty("java.io.tmpdir"));

        List items;
        try {
          items = upload.parseRequest(request);
        } catch (FileUploadBase.SizeLimitExceededException e) {
          return new FileTooLargeView();
        }

        // find the form fields first
        Iterator it = items.iterator();
        while (it.hasNext()) {
          FileItem item = (FileItem)it.next();
          if (item.isFormField() && item.getFieldName().startsWith("filename")) {
            int index = Integer.parseInt(item.getFieldName().substring(item.getFieldName().length()-1));
            filenames[index] = item.getString();
            log.debug("index is " + index + ", filename is " + filenames[index]);
          } else if (item.isFormField() && item.getFieldName().equals("path")) {
            path = item.getString();
          }
        }

        // now the actual files
        it = items.iterator();
        while (it.hasNext()) {
          FileItem item = (FileItem)it.next();

          if (!item.isFormField() && item.getSize() > 0 && item.getFieldName().startsWith("file")) {
            int index = Integer.parseInt(item.getFieldName().substring(item.getFieldName().length()-1));

            // if the filename hasn't been specified, use that from the file
            // being uploaded
            if (filenames[index] == null || filenames[index].length() == 0) {
              filenames[index] = item.getName();
            }

            File destinationDirectory = fileManager.getFile(path);
            File file = new File(destinationDirectory, filenames[index]);
            if (!fileManager.isUnderneathRootDirectory(file)) {
              response.setStatus(HttpServletResponse.SC_FORBIDDEN);
              return null;
            }

            long itemSize = item.getSize()/1024;
            if (FileManager.hasEnoughSpace(blog, itemSize)) {
              log.debug("Writing file " + filenames[index] + ", size is " + item.getSize());
              writeFile(fileManager, path, filenames[index], item);

              // if it's a theme file, also create a copy in blog.dir/theme
              if (type.equals(FileMetaData.THEME_FILE)) {
                writeFile(new FileManager(blog, FileMetaData.BLOG_DATA), "/theme" + path, filenames[index], item);
              }
            } else {
              return new NotEnoughSpaceView();
            }
          }
        }
      }

      blog.info("Files uploaded.");
    } catch (Exception e) {
      throw new ServletException(e);
    }

    FileMetaData directory = fileManager.getFileMetaData(path);

    return new RedirectView(blog.getUrl() + directory.getUrl());
  }
View Full Code Here

    String type = request.getParameter("type");
    String path = request.getParameter("path");
    String content = request.getParameter("fileContent");

    try {
      FileManager fileManager = new FileManager(blog, type);
      fileManager.saveFile(path, name, content);

      // if it's a theme file, also save a copy to blog.dir/theme
      if (type.equals(FileMetaData.THEME_FILE)) {
        fileManager = new FileManager(blog, FileMetaData.BLOG_DATA);
        fileManager.saveFile("/theme" + path, name, content);
      }

      blog.info("File \"" + StringUtils.transformHTML(name) + "\" saved.");
    } catch (IllegalFileAccessException e) {
      return new ForbiddenView();
View Full Code Here

*/
public class FileMetaDataComparatorTest extends SingleBlogTestCase {

  public void testCompare() {
    FileMetaDataComparator comp = new FileMetaDataComparator();
    FileManager fileManager = new FileManager(blog, FileMetaData.BLOG_FILE);
    FileMetaData f1 = fileManager.getFileMetaData("/", "a");
    FileMetaData f2 = fileManager.getFileMetaData("/", "b");

    assertTrue(comp.compare(f1, f1) == 0);
    assertTrue(comp.compare(f1, f2) < 0);
    assertTrue(comp.compare(f2, f1) > 0);
  }
View Full Code Here

    Blog blog = (Blog)getModel().get(Constants.BLOG_KEY);
    String name = StringUtils.filterHTML(request.getParameter("name"));
    String type = request.getParameter("type");
    String path = request.getParameter("path");

    FileManager fileManager = new FileManager(blog, type);
    FileMetaData directory = fileManager.getFileMetaData(path);
    try {
      fileManager.createDirectory(path, name);

      // if it's a theme file, also create a directory in blog.dir/theme
      if (type.equals(FileMetaData.THEME_FILE)) {
        fileManager = new FileManager(blog, FileMetaData.BLOG_DATA);
        fileManager.createDirectory("/theme" + path, name);
      }

      blog.info("Directory \"" + name + "\" created.");
    } catch (IllegalFileAccessException e) {
      return new ForbiddenView();
View Full Code Here

    String type = request.getParameter("type");
    String path = request.getParameter("path");
    String newName = StringUtils.filterHTML(request.getParameter("newName"));
    String submit = request.getParameter("submit");

    FileManager fileManager = new FileManager(blog, type);
    FileMetaData directory = fileManager.getFileMetaData(path);
    try {
      if (submit.equalsIgnoreCase("rename")) {
        fileManager.renameFile(path, name, newName);

        // if it's a theme file, also rename the copy in blog.dir/theme
        if (type.equals(FileMetaData.THEME_FILE)) {
          fileManager = new FileManager(blog, FileMetaData.BLOG_DATA);
          fileManager.renameFile("/theme" + path, name, newName);
        }

        blog.info("File \"" + oldName + "\" renamed to \"" + newName + "\".");
      } else {
        if (FileManager.hasEnoughSpace(blog, fileManager.getFileMetaData(path, name).getSizeInKB())) {
          fileManager.copyFile(path, name, newName);

          // if it's a theme file, also create a copy in blog.dir/theme
          if (type.equals(FileMetaData.THEME_FILE)) {
            fileManager = new FileManager(blog, FileMetaData.BLOG_DATA);
            fileManager.copyFile("/theme" + path, name, newName);
          }

          blog.info("File \"" + oldName + "\" copied to \"" + newName + "\".");
        } else {
          return new NotEnoughSpaceView();
View Full Code Here

TOP

Related Classes of net.sourceforge.pebble.domain.FileManager

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.