Skip to content

Commit

Permalink
Merge pull request #165 from SDCISA-13736-FixBadHabitsIn-FileSystemSt…
Browse files Browse the repository at this point in the history
…orage
  • Loading branch information
hiddenalpha authored Jan 3, 2024
2 parents 1b13c2f + 4fba5af commit db495db
Showing 1 changed file with 58 additions and 35 deletions.
93 changes: 58 additions & 35 deletions src/main/java/org/swisspush/reststorage/FileSystemStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public FileSystemStorage(Vertx vertx, String root) {

// Cache string length of root without trailing slashes
int rootLen;
for( rootLen=tmpRoot.length()-1 ; tmpRoot.charAt(rootLen) == File.separatorChar ; --rootLen );
for( rootLen=tmpRoot.length()-1 ; tmpRoot.charAt(rootLen) == '/' ; --rootLen );
this.rootLen = rootLen;
}

Expand All @@ -61,45 +61,68 @@ public void get(String path, String etag, final int offset, final int count, fin
final String fullPath = canonicalize(path);
log.debug("GET {}", path);
fileSystem().exists(fullPath, booleanAsyncResult -> {
if (booleanAsyncResult.result()) {
fileSystem().props(fullPath, filePropsAsyncResult -> {
final FileProps props = filePropsAsyncResult.result();
if (props.isDirectory()) {
log.debug("Delegate directory listing of '{}'", path);
fileSystemDirLister.handleListingRequest(path, offset, count, handler);
} else if (props.isRegularFile()) {
log.debug("Open file '{}' ({})", path, fullPath);
fileSystem().open(fullPath, OPEN_OPTIONS_READ_ONLY, event1 -> {
DocumentResource d = new DocumentResource();
if (event1.failed()) {
log.warn("Failed to open '{}' for read", path, event1.cause());
d.error = true;
d.errorMessage = event1.cause().getMessage();
} else {
log.debug("Successfully opened '{}' which is {} bytes in size.", path, props.size());
d.length = props.size();
d.readStream = new LoggingFileReadStream(d.length, path, event1.result());
d.closeHandler = v -> {
log.debug("Resource got closed. Close file now '{}'", path);
event1.result().close();
};
}
handler.handle(d);
});
} else {
// Is it a link maybe? Block device? Char device?
log.warn("Unknown filetype. Report 'no such file' for '{}'", path);
Resource r = new Resource();
r.exists = false;
handler.handle(r);
}
});
} else {
if( booleanAsyncResult.failed() ){
String msg = "vertx.fileSystem().exists()";
if( log.isWarnEnabled() ){
log.warn(msg, new Exception(fullPath, booleanAsyncResult.cause()));
}
Resource r = new Resource();
r.error = true;
r.errorMessage = msg +": "+ booleanAsyncResult.cause().getMessage();
handler.handle(r);
return;
}
var result = booleanAsyncResult.result();
if( result == null || result == false ){
log.debug("No such file '{}' ({})", path, fullPath);
Resource r = new Resource();
r.exists = false;
handler.handle(r);
return;
}
fileSystem().props(fullPath, filePropsAsyncResult -> {
if( filePropsAsyncResult.failed() ){
String msg = "vertx.fileSystem().props()";
if( log.isWarnEnabled() ){
log.warn(msg, new Exception(fullPath, filePropsAsyncResult.cause()));
}
Resource r = new Resource();
r.error = true;
r.errorMessage = msg +": "+ filePropsAsyncResult.cause().getMessage();
handler.handle(r);
return;
}
final FileProps props = filePropsAsyncResult.result();
if (props.isDirectory()) {
log.debug("Delegate directory listing of '{}'", path);
fileSystemDirLister.handleListingRequest(path, offset, count, handler);
} else if (props.isRegularFile()) {
log.debug("Open file '{}' ({})", path, fullPath);
fileSystem().open(fullPath, OPEN_OPTIONS_READ_ONLY, event1 -> {
DocumentResource d = new DocumentResource();
if (event1.failed()) {
log.warn("Failed to open '{}' for read", path, event1.cause());
d.error = true;
d.errorMessage = event1.cause().getMessage();
} else {
log.debug("Successfully opened '{}' which is {} bytes in size.", path, props.size());
d.length = props.size();
d.readStream = new LoggingFileReadStream<>(d.length, path, event1.result());
d.closeHandler = v -> {
log.debug("Resource got closed. Close file now '{}'", path);
event1.result().close();
};
}
handler.handle(d);
});
} else {
// Is it a link maybe? Block device? Char device?
log.warn("Unknown filetype. Report 'no such file' for '{}'", path);
Resource r = new Resource();
r.exists = false;
handler.handle(r);
}
});
});
}

Expand Down

0 comments on commit db495db

Please sign in to comment.