use of com.epam.pipeline.controller.vo.UploadFileMetadata in project cloud-pipeline by epam.
the class GitManagerTest method shouldRenameFile.
@Test
public void shouldRenameFile() throws GitClientException, UnsupportedEncodingException {
final Pipeline pipeline = testingPipeline();
final UploadFileMetadata file = new UploadFileMetadata();
final byte[] readmeContent = "Some inconsiderable content".getBytes("UTF-8");
file.setFileName(README_FILE);
file.setFileSize(readmeContent.length / 1024 + " Kb");
file.setFileType("text/markdown; charset=UTF-8");
file.setBytes(readmeContent);
final GitCommitEntry expectedCommit = new GitCommitEntry();
expectedCommit.setMessage("Rename the file");
expectedCommit.setCreatedAt("2017-07-25T13:13:11Z");
givenThat(post(urlPathEqualTo(api(REPOSITORY_COMMITS))).willReturn(okJson(with(expectedCommit))));
final GitFile gitFile = new GitFile();
gitFile.setContent(Base64.getEncoder().encodeToString(FILE_CONTENT.getBytes()));
givenThat(get(urlPathEqualTo(api(REPOSITORY_FILES))).withQueryParam(FILE_PATH, equalTo(DOCS + "/" + README_FILE)).withQueryParam(REF, equalTo(GIT_MASTER_REPOSITORY)).willReturn(okJson(with(gitFile))));
final GitCommitEntry resultingCommit = gitManager.uploadFiles(pipeline, DOCS, singletonList(file), pipeline.getCurrentVersion().getCommitId(), "Rename the file");
assertThat(resultingCommit, is(expectedCommit));
}
use of com.epam.pipeline.controller.vo.UploadFileMetadata in project cloud-pipeline by epam.
the class PipelineController method uploadFile.
@RequestMapping(value = "/pipeline/{id}/file/upload", method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "Uploads a file.", notes = "Uploads a file.", produces = MediaType.APPLICATION_JSON_VALUE)
public List<UploadFileMetadata> uploadFile(@PathVariable(value = ID) Long id, @RequestParam(value = PATH) final String folder, HttpServletRequest request) throws GitClientException, FileUploadException {
MultipartFile file = consumeMultipartFile(request);
List<UploadFileMetadata> uploadedFiles = new LinkedList<>();
UploadFileMetadata fileMeta = new UploadFileMetadata();
fileMeta.setFileName(FilenameUtils.getName(file.getOriginalFilename()).replaceAll("[ ]", "_"));
fileMeta.setFileSize(file.getSize() / BYTES_IN_KB + " Kb");
fileMeta.setFileType(file.getContentType());
try {
fileMeta.setBytes(file.getBytes());
uploadedFiles.add(fileMeta);
} catch (IOException e) {
LOGGER.debug(e.getMessage(), e);
}
pipelineApiService.uploadFiles(id, folder, uploadedFiles);
uploadedFiles.forEach(f -> f.setBytes(null));
return uploadedFiles;
}
use of com.epam.pipeline.controller.vo.UploadFileMetadata in project cloud-pipeline by epam.
the class GitManager method uploadFiles.
public GitCommitEntry uploadFiles(Pipeline pipeline, String folder, List<UploadFileMetadata> files, String lastCommitId, String commitMessage) throws GitClientException {
Assert.isTrue(lastCommitId.equals(pipeline.getCurrentVersion().getCommitId()), messageHelper.getMessage(MessageConstants.ERROR_REPOSITORY_WAS_UPDATED));
GitlabClient gitlabClient = this.getGitlabClientForPipeline(pipeline);
GitPushCommitEntry gitPushCommitEntry = new GitPushCommitEntry();
for (UploadFileMetadata file : files) {
String filePath = folder + PATH_DELIMITER + file.getFileName();
Arrays.stream(filePath.split(PATH_DELIMITER)).forEach(pathPart -> Assert.isTrue(GitUtils.checkGitNaming(pathPart), messageHelper.getMessage(MessageConstants.ERROR_INVALID_PIPELINE_FILE_NAME, filePath)));
boolean fileExists = false;
try {
fileExists = gitlabClient.getFileContents(filePath, GIT_MASTER_REPOSITORY) != null;
} catch (HttpClientErrorException exception) {
LOGGER.debug(exception.getMessage(), exception);
}
GitPushCommitActionEntry gitPushCommitActionEntry = new GitPushCommitActionEntry();
commitMessage = getCommitMessage(commitMessage, filePath, fileExists, gitPushCommitActionEntry);
gitPushCommitActionEntry.setFilePath(filePath);
gitPushCommitActionEntry.setContent(Base64.getEncoder().encodeToString(file.getBytes()));
gitPushCommitActionEntry.setEncoding(BASE64_ENCODING);
gitPushCommitEntry.getActions().add(gitPushCommitActionEntry);
}
gitPushCommitEntry.setCommitMessage(commitMessage);
return gitlabClient.commit(gitPushCommitEntry);
}
use of com.epam.pipeline.controller.vo.UploadFileMetadata in project cloud-pipeline by epam.
the class DataStorageController method uploadFile.
@RequestMapping(value = "/datastorage/{id}/list/upload", method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "Uploads a file to data storage.", notes = "Uploads a file to data storage.", produces = MediaType.APPLICATION_JSON_VALUE)
public List<UploadFileMetadata> uploadFile(@PathVariable(value = ID) Long id, @RequestParam(value = PATH, required = false) final String folder, HttpServletRequest request) throws FileUploadException {
MultipartFile file = consumeMultipartFile(request);
LinkedList<UploadFileMetadata> uploadedFiles = new LinkedList<>();
UploadFileMetadata fileMeta = new UploadFileMetadata();
fileMeta.setFileName(FilenameUtils.getName(file.getOriginalFilename()));
fileMeta.setFileSize(file.getSize() / BYTES_IN_KB + " Kb");
fileMeta.setFileType(file.getContentType());
try {
fileMeta.setBytes(file.getBytes());
uploadedFiles.add(fileMeta);
} catch (IOException e) {
throw new DataStorageException("Failed to upload file to datastorage.", e);
}
dataStorageApiService.createDataStorageFile(id, folder, fileMeta.getFileName(), fileMeta.getBytes());
return uploadedFiles;
}
Aggregations