@Override public File downloadFile(String url, String folderName, String fileName) { try { File folder = new File(fileStore.getStorageDir(), folderName); folder.mkdir(); try (InputStream in = new URL(url).openStream()) { String filename = folderName + '/' + fileName; return fileStore.store(in, filename); } } catch (IOException e) { throw new UncheckedIOException(e); } } }
private List<String> buildMissingRequiredFiles(String appDirectoryName) { List<String> missingFromArchive = newArrayList(); File indexFile = fileStore.getFile(appDirectoryName + separator + ZIP_INDEX_FILE); if (!indexFile.exists()) { missingFromArchive.add(ZIP_INDEX_FILE); } File configFile = fileStore.getFile(appDirectoryName + separator + ZIP_CONFIG_FILE); if (!configFile.exists()) { missingFromArchive.add(ZIP_CONFIG_FILE); } return missingFromArchive; }
@Override public String uploadApp(InputStream zipData, String zipFileName, String formFieldName) throws IOException { String tempFilesDir = "extracted_" + zipFileName; String tempAppDirectoryName = APPS_TMP_DIR + separator + tempFilesDir; fileStore.createDirectory(tempAppDirectoryName); try { ZipFileUtil.unzip(zipData, fileStore.getFile(tempAppDirectoryName)); } catch (UnzipException unzipException) { fileStore.delete(tempAppDirectoryName); throw new InvalidAppArchiveException(formFieldName, unzipException); } List<String> missingRequiredFilesList = buildMissingRequiredFiles(tempAppDirectoryName); if (!missingRequiredFilesList.isEmpty()) { fileStore.deleteDirectory(APPS_TMP_DIR); throw new AppArchiveMissingFilesException(missingRequiredFilesList); } return tempAppDirectoryName; }
@Override public AppConfig checkAndObtainConfig(String tempDir, String configContent) throws IOException { if (configContent.isEmpty() || !isConfigContentValidJson(configContent)) { fileStore.deleteDirectory(APPS_TMP_DIR); throw new InvalidAppConfigException(); } AppConfig appConfig = gson.fromJson(configContent, AppConfig.class); List<String> missingAppConfigParams = buildMissingConfigParams(appConfig); if (!missingAppConfigParams.isEmpty()) { fileStore.deleteDirectory(APPS_TMP_DIR); throw new AppConfigMissingParametersException(missingAppConfigParams); } if (appConfig.getName().contains("/")) { fileStore.deleteDirectory(APPS_TMP_DIR); throw new IllegalAppNameException(appConfig.getName()); } if (fileStore.getFile(APPS_DIR + separator + appConfig.getName()).exists()) { fileStore.deleteDirectory(APPS_TMP_DIR); throw new AppAlreadyExistsException(appConfig.getName()); } fileStore.move(tempDir, APPS_DIR + separator + appConfig.getName()); fileStore.deleteDirectory(APPS_TMP_DIR); return appConfig; }
private File fileLocationToStoredRenamedFile(String fileLocation, String entityTypeId) throws IOException { String filename = fileLocation.substring(fileLocation.lastIndexOf('/') + 1); URL url = new URL(fileLocation); try (InputStream is = url.openStream()) { return fileStore.store(is, getFilename(filename, entityTypeId)); } }
private FileMeta createStyleSheetFileMeta(String fileName, InputStream data) throws MolgenisStyleException { String fileId = idGenerator.generateId(); try { fileStore.store(data, fileId); } catch (IOException e) { throw new MolgenisStyleException("Unable to save style file with name : " + fileName, e); } FileMeta fileMeta = fileMetaFactory.create(fileId); fileMeta.setContentType("css"); fileMeta.setFilename(fileName); fileMeta.setSize(fileStore.getFile(fileId).length()); fileMeta.setUrl(buildFileUrl(fileId)); dataService.add(FileMetaMetaData.FILE_META, fileMeta); return fileMeta; }
if (appSourceArchive != null) File fileStoreFile = fileStore.getFile(appSourceArchive.getId()); if (fileStoreFile == null) fileStore.getStorageDir() + separatorChar + FILE_STORE_PLUGIN_APPS_PATH + separatorChar + app.getId() + separatorChar);
.addResourceHandler("/" + PATH_SEGMENT_APPS + "/**") .addResourceLocations( "file:///" + fileStore.getStorageDir() + '/' + FILE_STORE_PLUGIN_APPS_PATH + '/'); registry .addResourceHandler("/webjars/**")
private void deactivateApp(App app) { try { //noinspection StringConcatenationMissingWhitespace fileStore.deleteDirectory(FILE_STORE_PLUGIN_APPS_PATH + separatorChar + app.getId()); } catch (IOException e) { LOG.error("", e); throw new RuntimeException(format("An error occurred deactivating app '%s'", app.getName())); } }
public AppManagerServiceImpl( AppFactory appFactory, DataService dataService, FileStore fileStore, Gson gson, PluginFactory pluginFactory) { this.appFactory = requireNonNull(appFactory); this.dataService = requireNonNull(dataService); this.fileStore = requireNonNull(fileStore); this.gson = requireNonNull(gson); this.pluginFactory = requireNonNull(pluginFactory); fileStore.createDirectory(APPS_DIR); fileStore.createDirectory(APPS_TMP_DIR); }
@Bean public FileStore fileStore() { // get molgenis home directory Path appDataRoot = AppDataRootProvider.getAppDataRoot(); // create molgenis store directory in molgenis data directory if not exists String molgenisFileStoreDirStr = Paths.get(appDataRoot.toString(), "data", "filestore").toString(); File molgenisDataDir = new File(molgenisFileStoreDirStr); if (!molgenisDataDir.exists() && !molgenisDataDir.mkdirs()) { throw new UncheckedIOException( new IOException("failed to create directory: " + molgenisFileStoreDirStr)); } return new FileStore(molgenisFileStoreDirStr); }
@ResponseBody @PostMapping(value = "/upload", produces = MediaType.TEXT_HTML_VALUE) public String importFile(@RequestParam(value = "file") MultipartFile multipartFile) throws IOException { String filename = multipartFile.getOriginalFilename(); try (InputStream inputStream = multipartFile.getInputStream()) { fileStore.store(inputStream, filename); } OneClickImportJobExecution jobExecution = oneClickImportJobExecutionFactory.create(); jobExecution.setFile(filename); jobExecutor.submit(jobExecution); return concatEntityHref(jobExecution); }
private File storeFile( FileStore fileStore, String key, String extension, String targetEntityName, String jobIdentifier, InputStream in) throws IOException { // no extension given, default is excel if (!StringUtils.isNotEmpty(extension)) { extension = "xlsx"; } String relativePath = "bucket_" + jobIdentifier; File folder = new File(fileStore.getStorageDir(), relativePath); folder.mkdir(); String filename; if (StringUtils.isNotEmpty(targetEntityName) && !ExcelFileExtensions.getExcel().contains(extension)) { filename = String.format("%s%s%s.%s", relativePath, File.separatorChar, targetEntityName, extension); } else { String fileRoot = key.replaceAll("[\\/:*?\"<>|]", "_"); filename = String.format("%s%s%s.%s", relativePath, File.separatorChar, fileRoot, extension); } return fileStore.store(in, filename); }
@Override public String extractFileContent(String appDir, String fileName) { File indexFile = fileStore.getFile(appDir + separator + fileName); return utf8Encodedfiletostring(indexFile); }
File uploadFile = fileStore.store(inputStream, sessionId + ".csv"); String inputRepositoryName = idGenerator.generateId(); SortaCsvRepository inputRepository =
/** Upload a new molgenis logo */ @PreAuthorize("hasAnyRole('ROLE_SU')") @PostMapping("/upload-logo") public String uploadLogo(@RequestParam("logo") Part part, Model model) throws IOException { String contentType = part.getContentType(); if ((contentType == null) || !contentType.startsWith("image")) { model.addAttribute("errorMessage", ERRORMESSAGE_LOGO); } else { // Create the logo subdir in the filestore if it doesn't exist File logoDir = new File(fileStore.getStorageDir() + "/logo"); if (!logoDir.exists()) { if (!logoDir.mkdir()) { throw new IOException("Unable to create directory [" + logoDir.getAbsolutePath() + "]"); } } // Store the logo in the logo dir of the filestore String file = "/logo/" + FileUploadUtils.getOriginalFileName(part); try (InputStream inputStream = part.getInputStream()) { fileStore.store(inputStream, file); } // Set logo appSettings.setLogoNavBarHref(file); } return init(model); } }
private void serveAppResource( HttpServletResponse response, String wildCardPath, AppResponse appResponse) throws IOException { File requestedResource = fileStore.getFile(appResponse.getResourceFolder() + wildCardPath); response.setContentType(guessMimeType(requestedResource.getName())); response.setContentLength((int) requestedResource.length()); response.setHeader( CONTENT_DISPOSITION, "attachment; filename=" + requestedResource.getName().replace(" ", "_")); try (InputStream is = new FileInputStream(requestedResource)) { FileCopyUtils.copy(is, response.getOutputStream()); } }
File tmpFile; try (InputStream inputStream = file.getInputStream()) { tmpFile = fileStore.store(inputStream, filename);
/** Get a file from the logo subdirectory of the filestore */ @GetMapping("/logo/{name}.{extension}") public void getLogo( OutputStream out, @PathVariable("name") String name, @PathVariable("extension") String extension, HttpServletResponse response) throws IOException { File f = fileStore.getFile("/logo/" + name + "." + extension); if (!f.exists()) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } // Try to get contenttype from file String contentType = URLConnection.guessContentTypeFromName(f.getName()); if (contentType != null) { response.setContentType(contentType); } FileCopyUtils.copy(new FileInputStream(f), out); } }
fileStore.store(inputStream, id); } catch (IOException e) { throw new MolgenisDataException(e);