public JSONObject getJsonResponse() throws Exception { JSONObject json = new JSONObject(); if (email == null || userSpace == null) { json.put("status", "error"); json.put("error", "Usuari incorrecte"); } else { json.put("status", status); if (status.equals("ok")) { json.put("project", prj.getJSON()); } else if (err != null) { json.put("error", err); } } return json; }
protected void processUploadedFile(FileItem item, String projectName) throws Exception { String fieldName = item.getFieldName(); String fileName = item.getName(); String contentType = item.getContentType(); // boolean isInMemory = item.isInMemory(); long sizeInBytes = item.getSize(); if (fieldName == null || !fieldName.equals("scormFile") || fileName == null || !fileName.endsWith(".scorm.zip") || !"application/zip".equals(contentType)) { status = "error"; err = "Tipus de fitxer incorrecte!"; } else if (sizeInBytes > (quota - userSpace.currentSize)) { status = "error"; err = "Heu excedit l'espai disponible!"; } else { // Get a valid project name if (projectName == null || "".equals(projectName)) { projectName = fileName.substring(0, fileName.indexOf('.')); } projectName = UserProject.getValidName(projectName); // Reset project if exists userSpace.removeProject(projectName); prj = new UserProject(projectName, userSpace); String prjBase = prj.prjRoot.getCanonicalPath() + File.separator; // Treat input as a ZIP stream ZipInputStream zis = new ZipInputStream(new BufferedInputStream(item.getInputStream())); ZipEntry entry; boolean interrupted = false; int fileCount = 0; while ((entry = zis.getNextEntry()) != null) { // Process files File entryFile = new File(prj.prjRoot, entry.getName()); if (!entryFile.getCanonicalPath().startsWith(prjBase)) { // File is out of project's directory prj.clean(); interrupted = true; break; } if (entry.isDirectory()) { entryFile.mkdirs(); } else { entryFile.getParentFile().mkdirs(); IOUtils.copy(zis, new FileOutputStream(entryFile)); fileCount++; } } zis.close(); // Add project to collection userSpace.addProject(prj); if (interrupted) { status = "error"; err = "Nom de fitxer incorrecte dins del fitxer ZIP"; } else if (fileCount == 0) { status = "error"; err = "El fitxer no tenia cap contingut vàlid"; } else { status = "ok"; } } }