mirror of
https://github.com/imgfloat/server.git
synced 2026-02-05 03:39:26 +00:00
Add missing transactions
This commit is contained in:
@@ -0,0 +1,61 @@
|
|||||||
|
package dev.kruhlmann.imgfloat.controller;
|
||||||
|
|
||||||
|
import dev.kruhlmann.imgfloat.model.ErrorResponse;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.HttpStatusCode;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
|
|
||||||
|
@RestControllerAdvice
|
||||||
|
public class RestExceptionHandler {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(RestExceptionHandler.class);
|
||||||
|
|
||||||
|
@ExceptionHandler(ResponseStatusException.class)
|
||||||
|
public ResponseEntity<ErrorResponse> handleStatusException(
|
||||||
|
ResponseStatusException exception,
|
||||||
|
HttpServletRequest request
|
||||||
|
) {
|
||||||
|
String path = request.getRequestURI();
|
||||||
|
HttpStatusCode statusCode = exception.getStatusCode();
|
||||||
|
LOG.warn(
|
||||||
|
"Request {} {} failed with status {}: {}",
|
||||||
|
request.getMethod(),
|
||||||
|
path,
|
||||||
|
statusCode.value(),
|
||||||
|
exception.getReason(),
|
||||||
|
exception
|
||||||
|
);
|
||||||
|
String message = exception.getReason();
|
||||||
|
if (message == null || message.isBlank()) {
|
||||||
|
message = "Request failed with status " + statusCode.value();
|
||||||
|
}
|
||||||
|
return ResponseEntity.status(statusCode).body(new ErrorResponse(statusCode.value(), message, path));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(Exception.class)
|
||||||
|
public ResponseEntity<ErrorResponse> handleUnexpectedException(Exception exception, HttpServletRequest request) {
|
||||||
|
String path = request.getRequestURI();
|
||||||
|
LOG.error(
|
||||||
|
"Unhandled exception while processing {} {}: {}",
|
||||||
|
request.getMethod(),
|
||||||
|
path,
|
||||||
|
exception.getMessage(),
|
||||||
|
exception
|
||||||
|
);
|
||||||
|
return ResponseEntity
|
||||||
|
.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||||
|
.body(
|
||||||
|
new ErrorResponse(
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR.value(),
|
||||||
|
"An unexpected error occurred while handling the request.",
|
||||||
|
path
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package dev.kruhlmann.imgfloat.model;
|
||||||
|
|
||||||
|
public record ErrorResponse(int status, String message, String path) {}
|
||||||
@@ -316,6 +316,7 @@ public class ChannelDirectoryService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional(rollbackFor = IOException.class)
|
||||||
public Optional<AssetView> createAsset(String broadcaster, MultipartFile file, String actor) throws IOException {
|
public Optional<AssetView> createAsset(String broadcaster, MultipartFile file, String actor) throws IOException {
|
||||||
long fileSize = file.getSize();
|
long fileSize = file.getSize();
|
||||||
if (fileSize > uploadLimitBytes) {
|
if (fileSize > uploadLimitBytes) {
|
||||||
@@ -412,6 +413,7 @@ public class ChannelDirectoryService {
|
|||||||
return Optional.of(view);
|
return Optional.of(view);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public Optional<AssetView> createCodeAsset(String broadcaster, CodeAssetRequest request, String actor) {
|
public Optional<AssetView> createCodeAsset(String broadcaster, CodeAssetRequest request, String actor) {
|
||||||
validateCodeAssetSource(request.getSource());
|
validateCodeAssetSource(request.getSource());
|
||||||
Channel channel = getOrCreateChannel(broadcaster);
|
Channel channel = getOrCreateChannel(broadcaster);
|
||||||
@@ -459,6 +461,7 @@ public class ChannelDirectoryService {
|
|||||||
return Optional.of(view);
|
return Optional.of(view);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public Optional<AssetView> updateCodeAsset(String broadcaster, String assetId, CodeAssetRequest request, String actor) {
|
public Optional<AssetView> updateCodeAsset(String broadcaster, String assetId, CodeAssetRequest request, String actor) {
|
||||||
validateCodeAssetSource(request.getSource());
|
validateCodeAssetSource(request.getSource());
|
||||||
String normalized = normalize(broadcaster);
|
String normalized = normalize(broadcaster);
|
||||||
@@ -526,6 +529,7 @@ public class ChannelDirectoryService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional(rollbackFor = IOException.class)
|
||||||
public Optional<AssetView> updateScriptLogo(String broadcaster, String assetId, MultipartFile file, String actor)
|
public Optional<AssetView> updateScriptLogo(String broadcaster, String assetId, MultipartFile file, String actor)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
Asset asset = requireScriptAssetForBroadcaster(broadcaster, assetId);
|
Asset asset = requireScriptAssetForBroadcaster(broadcaster, assetId);
|
||||||
@@ -576,6 +580,7 @@ public class ChannelDirectoryService {
|
|||||||
return Optional.of(view);
|
return Optional.of(view);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public Optional<AssetView> clearScriptLogo(String broadcaster, String assetId, String actor) {
|
public Optional<AssetView> clearScriptLogo(String broadcaster, String assetId, String actor) {
|
||||||
Asset asset = requireScriptAssetForBroadcaster(broadcaster, assetId);
|
Asset asset = requireScriptAssetForBroadcaster(broadcaster, assetId);
|
||||||
ScriptAsset script = scriptAssetRepository
|
ScriptAsset script = scriptAssetRepository
|
||||||
@@ -809,6 +814,7 @@ public class ChannelDirectoryService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public Optional<AssetView> importMarketplaceScript(String targetBroadcaster, String scriptId, String actor) {
|
public Optional<AssetView> importMarketplaceScript(String targetBroadcaster, String scriptId, String actor) {
|
||||||
Optional<MarketplaceScriptSeedLoader.SeedScript> seedScript = marketplaceScriptSeedLoader.findById(scriptId);
|
Optional<MarketplaceScriptSeedLoader.SeedScript> seedScript = marketplaceScriptSeedLoader.findById(scriptId);
|
||||||
Optional<AssetView> imported;
|
Optional<AssetView> imported;
|
||||||
@@ -1014,6 +1020,7 @@ public class ChannelDirectoryService {
|
|||||||
return SAFE_FILENAME.matcher(stripped).replaceAll("_");
|
return SAFE_FILENAME.matcher(stripped).replaceAll("_");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public Optional<AssetView> updateTransform(String broadcaster, String assetId, TransformRequest req, String actor) {
|
public Optional<AssetView> updateTransform(String broadcaster, String assetId, TransformRequest req, String actor) {
|
||||||
String normalized = normalize(broadcaster);
|
String normalized = normalize(broadcaster);
|
||||||
|
|
||||||
@@ -1240,6 +1247,7 @@ public class ChannelDirectoryService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public Optional<AssetView> updateVisibility(
|
public Optional<AssetView> updateVisibility(
|
||||||
String broadcaster,
|
String broadcaster,
|
||||||
String assetId,
|
String assetId,
|
||||||
@@ -1363,6 +1371,7 @@ public class ChannelDirectoryService {
|
|||||||
return loadScriptAttachments(normalize(broadcaster), asset.getId(), null);
|
return loadScriptAttachments(normalize(broadcaster), asset.getId(), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional(rollbackFor = IOException.class)
|
||||||
public Optional<ScriptAssetAttachmentView> createScriptAttachment(
|
public Optional<ScriptAssetAttachmentView> createScriptAttachment(
|
||||||
String broadcaster,
|
String broadcaster,
|
||||||
String scriptAssetId,
|
String scriptAssetId,
|
||||||
@@ -1446,6 +1455,7 @@ public class ChannelDirectoryService {
|
|||||||
return Optional.of(view);
|
return Optional.of(view);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public boolean deleteScriptAttachment(
|
public boolean deleteScriptAttachment(
|
||||||
String broadcaster,
|
String broadcaster,
|
||||||
String scriptAssetId,
|
String scriptAssetId,
|
||||||
|
|||||||
Reference in New Issue
Block a user