Beispiel #1
0
  public static void uploadRepo(Long id, @Required File repo, String module, String version)
      throws ZipException, IOException {
    models.Upload upload = Uploads.getUpload(id);
    File uploadsDir = Util.getUploadDir(upload.id);

    if (validationFailed()) {
      Uploads.uploadRepoForm(id);
    }

    String name = repo.getName().toLowerCase();

    if (name.endsWith(".zip")) uploadZip(repo, null, uploadsDir);
    else {
      boolean found = false;
      for (String postfix : SupportedExtensions) {
        if (name.endsWith(postfix)) {
          uploadArchive(repo, postfix, uploadsDir, module, version, id);
          found = true;
          break;
        }
      }
      if (!found)
        error(
            HttpURLConnection.HTTP_BAD_REQUEST,
            "Invalid uploaded file (must be zip, car, jar or src)");
    }

    Uploads.view(id);
  }
Beispiel #2
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);
  }
Beispiel #3
0
  public static void deleteFile(Long id, String path, boolean returnToBrowse) throws IOException {
    models.Upload upload = Uploads.getUpload(id);
    File uploadsDir = Util.getUploadDir(upload.id);
    File file = new File(uploadsDir, path);

    deleteFileImpl(path, file, uploadsDir, upload);

    if (returnToBrowse) {
      File parent = file.getParentFile();
      String parentPath = JavaExtensions.relativeTo(parent, upload);
      // if we do viewFile directly we get silly %2F escapes in the URL
      redirect(Util.viewPublicUploadUrl(upload, parentPath));
    } else {
      Uploads.view(id);
    }
  }
Beispiel #4
0
  public static void deleteFileAsync(Long id, String path) throws IOException {
    models.Upload upload = Uploads.getUpload(id);
    File uploadsDir = Util.getUploadDir(upload.id);
    File file = new File(uploadsDir, path);

    deleteFileImpl(path, file, uploadsDir, upload);

    String message = Scope.Flash.current().get("message");
    renderJSON("{\"message\":\"" + message + "\"}");
  }
Beispiel #5
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());
    }
  }
Beispiel #6
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();
    }
  }
Beispiel #7
0
 public static void uploadRepoForm(Long id) {
   models.Upload upload = Uploads.getUpload(id);
   render(upload);
 }