示例#1
0
  private static void deleteFileImpl(String path, File file, File uploadsDir, Upload upload)
      throws IOException {
    checkUploadPath(file, uploadsDir);

    if (!file.exists()) {
      notFound(path);
    }

    Logger.info("delete: %s exists: %s", path, file.exists());

    if (uploadsDir.getCanonicalPath().equals(file.getCanonicalPath())) {
      // clear the entire repo
      for (File f : uploadsDir.listFiles()) {
        if (f.isDirectory()) FileUtils.deleteDirectory(f);
        else f.delete();
      }
      // let's be helpful and remove maven dependencies too
      for (MavenDependency md : upload.mavenDependencies) {
        md.delete();
      }
      flash("message", "Upload cleared");
    } else if (file.isDirectory()) {
      FileUtils.deleteDirectory(file);
      flash("message", "Directory deleted");
    } else {
      file.delete();
      flash("message", "File deleted");
    }
  }
示例#2
0
  private static void uploadArchive(
      File file, String postfix, File uploadsDir, String module, String version, Long uploadId)
      throws IOException {
    try {
      // parse unless it's a jar and we have alternate info
      ModuleSpec spec;
      Logger.info("postfix: %s, module: %s, version: %s", postfix, module, version);
      if (postfix.equals(".jar")
          && (!StringUtils.isEmpty(module) || !StringUtils.isEmpty(version))) {
        Validation.required("module", module);
        if ("default".equals(module)) Validation.addError("module", "Default module not allowed");
        Validation.required("version", version);
        if (validationFailed()) {
          Uploads.uploadRepoForm(uploadId);
        }
        spec = new ModuleSpec(module, version);
      } else {
        spec = ModuleSpec.parse(file.getName(), postfix);
      }

      // build dest file
      String path =
          spec.name.replace('.', File.separatorChar)
              + File.separatorChar
              + spec.version
              + File.separatorChar
              + spec.name
              + '-'
              + spec.version
              + postfix;
      File destFile = new File(uploadsDir, path);

      // check
      checkUploadPath(destFile, uploadsDir);
      if (destFile.isDirectory())
        error(
            HttpURLConnection.HTTP_BAD_REQUEST,
            "Invalid path for upload: " + destFile.getName() + " (is a directory)");

      // all good, let's copy
      destFile.getParentFile().mkdirs();
      FileUtils.copyFile(file, destFile);

      // now let's sha1 it
      String sha1 = ModuleChecker.sha1(destFile);
      File sha1File = new File(uploadsDir, path + ".sha1");
      FileUtils.write(sha1File, sha1);

      flash("message", "Uploaded archive file");
    } catch (ModuleSpec.ModuleSpecException x) {
      error(HttpURLConnection.HTTP_BAD_REQUEST, x.getMessage());
    }
  }
示例#3
0
  public static void listFolder(Long id, String path) throws IOException {
    models.Upload upload = getUpload(id);
    File uploadsDir = Util.getUploadDir(upload.id);
    File file = new File(uploadsDir, path);
    Uploads.checkUploadPath(file, uploadsDir);

    if (!file.exists()) notFound(path);

    if (file.isDirectory()) {
      render("Uploads/listFolder.html", upload, file);
    } else {
      notFound();
    }
  }
示例#4
0
  public static void addChecksum(Long id, String path) throws IOException {
    models.Upload upload = Uploads.getUpload(id);
    File uploadsDir = Util.getUploadDir(upload.id);
    File file = new File(uploadsDir, path);
    checkUploadPath(file, uploadsDir);

    if (!file.exists()) notFound(path);

    Logger.info("add checksum: %s exists: %s", path, file.exists());

    String sha1 = ModuleChecker.sha1(file);
    File sha1File = new File(uploadsDir, path + ".sha1");
    FileUtils.write(sha1File, sha1);

    Uploads.view(id);
  }
示例#5
0
  static void uploadZip(File repo, String prefixFilter, File uploadsDir)
      throws ZipException, IOException {
    ZipFile zip = new ZipFile(repo);
    try {
      // first check them all
      Enumeration<? extends ZipEntry> entries = zip.entries();
      while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        // skip directories
        if (entry.isDirectory()) continue;
        if (prefixFilter != null && !entry.getName().toLowerCase().startsWith(prefixFilter))
          continue;
        File file = new File(uploadsDir, entry.getName());
        checkUploadPath(file, uploadsDir);
        if (file.isDirectory())
          error(
              HttpURLConnection.HTTP_BAD_REQUEST,
              "Invalid path for upload: " + entry.getName() + " (is a directory)");
      }
      // then store
      entries = zip.entries();
      int files = 0;
      while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        // skip directories
        if (entry.isDirectory()) continue;
        String targetName = entry.getName();
        if (prefixFilter != null) {
          if (!targetName.toLowerCase().startsWith(prefixFilter)) continue;
          targetName = targetName.substring(prefixFilter.length());
        }
        File file = new File(uploadsDir, targetName);

        files++;
        file.getParentFile().mkdirs();
        InputStream inputStream = zip.getInputStream(entry);
        try {
          FileUtils.copyInputStreamToFile(inputStream, file);
        } finally {
          inputStream.close();
        }
      }
      flash("message", "Uploaded " + files + " file" + (files > 1 ? "s" : ""));
    } finally {
      zip.close();
    }
  }