-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
212 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/java/com/github/rishabh9/kumoru/handlers/UploadHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.github.rishabh9.kumoru.handlers; | ||
|
||
import io.vertx.core.Vertx; | ||
import io.vertx.ext.web.RoutingContext; | ||
import lombok.extern.log4j.Log4j2; | ||
|
||
@Log4j2 | ||
public class UploadHandler extends KumoruHandler { | ||
|
||
private final Vertx vertx; | ||
|
||
public UploadHandler(final Vertx vertx) { | ||
this.vertx = vertx; | ||
} | ||
|
||
@Override | ||
public void handle(final RoutingContext routingContext) { | ||
final String path = routingContext.normalisedPath(); | ||
final String subPath = path.substring(0, path.lastIndexOf("/")); | ||
final String directory = REPO_ROOT + subPath; | ||
vertx | ||
.fileSystem() | ||
.exists( | ||
directory, | ||
existsResult -> { | ||
if (existsResult.succeeded() | ||
&& null != existsResult.result() | ||
&& existsResult.result()) { | ||
log.debug("The directory {} already exists", subPath); | ||
writeFile(routingContext); | ||
} else { | ||
log.debug("The directory {} does not exists", subPath); | ||
createDirectory(routingContext, directory); | ||
} | ||
}); | ||
} | ||
|
||
private void createDirectory(final RoutingContext routingContext, final String directory) { | ||
vertx | ||
.fileSystem() | ||
.mkdirs( | ||
directory, | ||
mkdirsResult -> { | ||
if (mkdirsResult.succeeded()) { | ||
log.debug("Created directory {}", directory); | ||
writeFile(routingContext); | ||
} else { | ||
log.error("Failed to create directory {}", directory, mkdirsResult.cause()); | ||
routingContext | ||
.response() | ||
.setStatusCode(INTERNAL_ERROR) | ||
.setStatusMessage(mkdirsResult.cause().getMessage()) | ||
.end(); | ||
} | ||
}); | ||
} | ||
|
||
private void writeFile(final RoutingContext routingContext) { | ||
final String path = routingContext.normalisedPath(); | ||
vertx | ||
.fileSystem() | ||
.writeFile( | ||
REPO_ROOT + path, | ||
routingContext.getBody(), | ||
writeResult -> { | ||
if (writeResult.succeeded()) { | ||
log.debug("Saved {} to disk", path); | ||
routingContext.response().end(); | ||
} else { | ||
log.error("There was error writing {} to disk", path, writeResult.cause()); | ||
routingContext | ||
.response() | ||
.setStatusCode(INTERNAL_ERROR) | ||
.setStatusMessage(writeResult.cause().getMessage()) | ||
.end(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters