Skip to content

Commit

Permalink
Remove transferring file
Browse files Browse the repository at this point in the history
  • Loading branch information
pbortnik committed Sep 11, 2017
1 parent 4baf038 commit 47eff3f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@
import com.epam.ta.reportportal.database.entity.Project;
import com.epam.ta.reportportal.events.ImportFinishedEvent;
import com.epam.ta.reportportal.events.ImportStartedEvent;
import com.epam.ta.reportportal.exception.ReportPortalException;
import com.epam.ta.reportportal.ws.model.ErrorType;
import com.epam.ta.reportportal.ws.model.OperationCompletionRS;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.IOException;

import static com.epam.ta.reportportal.commons.Predicates.notNull;
import static com.epam.ta.reportportal.commons.validation.BusinessRule.expect;
Expand All @@ -59,28 +59,23 @@ public class ImportLaunchHandlerImpl implements ImportLaunchHandler {
@Override
public OperationCompletionRS importLaunch(String projectId, String userName, String format, MultipartFile file) {
Project project = projectRepository.findOne(projectId);

expect(project, notNull()).verify(PROJECT_NOT_FOUND, projectId);
expect(file.getOriginalFilename(), it -> it.matches(ZIP_REGEX))
.verify(ErrorType.BAD_IMPORT_FILE_TYPE, file.getOriginalFilename());

String originalFilename = file.getOriginalFilename();
expect(originalFilename, it -> it.matches(ZIP_REGEX)).verify(ErrorType.BAD_IMPORT_FILE_TYPE, originalFilename);

ImportType type = ImportType.fromValue(format).orElse(null);
expect(type, notNull()).verify(ErrorType.BAD_IMPORT_FILE_TYPE, format);

ImportStrategy strategy = factory.getImportLaunch(type);
File tempFile = transferToTempFile(file);
eventPublisher.publishEvent(new ImportStartedEvent(projectId, userName, file.getOriginalFilename()));
String launch = strategy.importLaunch(projectId, userName, tempFile);
eventPublisher.publishEvent(new ImportFinishedEvent(projectId, userName, file.getOriginalFilename()));
return new OperationCompletionRS("Launch with id = " + launch + " is successfully imported.");
}
CommonsMultipartFile multipartFile = (CommonsMultipartFile) file;
DiskFileItem fileItem = (DiskFileItem) multipartFile.getFileItem();
File tempFile = fileItem.getStoreLocation();

eventPublisher.publishEvent(new ImportStartedEvent(projectId, userName, originalFilename));
String launch = strategy.importLaunch(projectId, userName, tempFile, originalFilename);
eventPublisher.publishEvent(new ImportFinishedEvent(projectId, userName, originalFilename));

private File transferToTempFile(MultipartFile file) {
try {
File tmp = File.createTempFile(file.getOriginalFilename(), ".zip");
file.transferTo(tmp);
return tmp;
} catch (IOException e) {
throw new ReportPortalException("Error during transferring multipart file", e);
}
return new OperationCompletionRS("Launch with id = " + launch + " is successfully imported.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@
* @author Pavel_Bortnik
*/
public interface ImportStrategy {
/**
* Processing launch importing.
*
* @param projectId project
* @param userName user
* @param file zip file that contains xml test reports
* @return launch id
*/
String importLaunch(String projectId, String userName, File file);
/**
* Processing launch importing.
*
* @param projectId project
* @param userName user
* @param file zip file that contains xml test reports
* @param originalFilename original file name
* @return launch id
*/
String importLaunch(String projectId, String userName, File file, String originalFilename);
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,22 @@ public class XunitImportStrategy implements ImportStrategy {
private static final Predicate<ZipEntry> isXml = zipEntry -> zipEntry.getName().matches(XML_REGEX);

@Override
public String importLaunch(String projectId, String userName, File file) {
try {
return processZipFile(file, projectId, userName);
} catch (IOException e) {
throw new ReportPortalException(ErrorType.BAD_IMPORT_FILE_TYPE, file.getName(), e);
}
public String importLaunch(String projectId, String userName, File file, String originalFilename) {
try {
return processZipFile(file, projectId, userName, originalFilename);
} catch (IOException e) {
throw new ReportPortalException(ErrorType.BAD_IMPORT_FILE_TYPE, file.getName(), e);
} finally {
if (null != file) {
file.delete();
}
}
}

private String processZipFile(File zip, String projectId, String userName) throws IOException {
try (ZipFile zipFile = new ZipFile(zip)) {
String launchId = startLaunch(projectId, userName, zip.getName().substring(0, zip.getName().indexOf(".zip")));
CompletableFuture[] futures = zipFile.stream()
private String processZipFile(File zip, String projectId, String userName, String fileName) throws IOException {
try (ZipFile zipFile = new ZipFile(zip)) {
String launchId = startLaunch(projectId, userName, fileName);
CompletableFuture[] futures = zipFile.stream()
.filter(isFile.and(isXml))
.map(zipEntry -> {
try {
Expand Down

0 comments on commit 47eff3f

Please sign in to comment.