Delete our copies of data if any exception is thrown.

This commit is contained in:
CalDescent 2021-07-04 13:39:00 +01:00
parent a742fecf9c
commit 6407b5452b
2 changed files with 17 additions and 8 deletions

View File

@ -257,8 +257,6 @@ public class ArbitraryResource {
if (Settings.getInstance().isApiRestricted())
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.NON_PRODUCTION);
try (final Repository repository = RepositoryManager.getRepository()) {
// Check if a file or directory has been supplied
File file = new File(path);
if (!file.isFile()) {
@ -267,6 +265,12 @@ public class ArbitraryResource {
}
DataFile dataFile = new DataFile(path);
if (dataFile == null) {
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA);
}
try (final Repository repository = RepositoryManager.getRepository()) {
DataFile.ValidationResult validationResult = dataFile.isValid();
if (validationResult != DataFile.ValidationResult.OK) {
LOGGER.error("Invalid file: {}", validationResult);
@ -319,11 +323,14 @@ public class ArbitraryResource {
return Base58.encode(bytes);
} catch (DataException e) {
dataFile.deleteAll();
LOGGER.error("Repository issue when uploading data", e);
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE, e);
} catch (TransformationException e) {
dataFile.deleteAll();
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.TRANSFORMATION_ERROR, e);
} catch (IllegalStateException e) {
dataFile.deleteAll();
LOGGER.error("Invalid upload data", e);
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.INVALID_DATA, e);
}

View File

@ -139,8 +139,10 @@ public class WebsiteResource {
return Base58.encode(bytes);
} catch (TransformationException e) {
dataFile.deleteAll();
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.TRANSFORMATION_ERROR, e);
} catch (DataException e) {
dataFile.deleteAll();
throw ApiExceptionFactory.INSTANCE.createException(request, ApiError.REPOSITORY_ISSUE, e);
}
}