Skip to content

Commit a6da5fe

Browse files
committed
feat: implement a method to delete a directory from a template.
This change allows developers to directly delete a directory and all its containing files and directories from a given service template. As an additional functionality, this method is also exposed to the HTTP Rest API much like the file deletion method already is.
1 parent b1ce28e commit a6da5fe

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

driver/src/main/java/eu/cloudnetservice/driver/template/TemplateStorage.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,17 @@ default boolean deployDirectory(@NonNull ServiceTemplate target, @NonNull Path d
220220
*/
221221
boolean deleteFile(@NonNull ServiceTemplate template, @NonNull String path);
222222

223+
/**
224+
* Deletes the given directory at the given path in the given template in this storage.
225+
* This method recursively deletes all files and directories inside the given directory.
226+
*
227+
* @param template the template in which the directory to delete is located in.
228+
* @param path the path to the directory in the template to delete.
229+
* @return true if the directory at the given path was deleted successfully, false otherwise.
230+
* @throws NullPointerException if the given template or path is null.
231+
*/
232+
boolean deleteDirectory(@NonNull ServiceTemplate template, @NonNull String path);
233+
223234
/**
224235
* Opens a new input stream to read the content of the file at the given path in the given template in this storage.
225236
* This method returns null if either the file doesn't exist or is a directory.
@@ -480,6 +491,19 @@ default boolean deployDirectory(@NonNull ServiceTemplate target, @NonNull Path d
480491
return Task.supply(() -> this.deleteFile(template, path));
481492
}
482493

494+
/**
495+
* Deletes the given directory at the given path in the given template in this storage. This method recursively
496+
* deletes all files and directories inside the given directory.
497+
*
498+
* @param template the template in which the directory to delete is located in.
499+
* @param path the path to the directory in the template to delete.
500+
* @return a task completed with true if the directory at the given path was deleted successfully, false otherwise.
501+
* @throws NullPointerException if the given template or path is null.
502+
*/
503+
default @NonNull Task<Boolean> deleteDirectoryAsync(@NonNull ServiceTemplate template, @NonNull String path) {
504+
return Task.supply(() -> this.deleteDirectory(template, path));
505+
}
506+
483507
/**
484508
* Opens a new input stream to read the content of the file at the given path in the given template in this storage.
485509
* This method returns null if either the file doesn't exist or is a directory.

modules/rest/src/main/java/eu/cloudnetservice/modules/rest/v2/V2HttpHandlerTemplate.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,25 @@ private void handleFileDeleteRequest(
238238
});
239239
}
240240

241+
@BearerAuth
242+
@HttpRequestHandler(paths = "/api/v2/template/{storage}/{prefix}/{name}/directory", methods = "DELETE")
243+
private void handleDirectoryDeleteRequest(
244+
@NonNull HttpContext context,
245+
@NonNull @RequestPathParam("storage") String storageName,
246+
@NonNull @RequestPathParam("prefix") String prefix,
247+
@NonNull @RequestPathParam("name") String templateName,
248+
@NonNull @FirstRequestQueryParam("path") String path
249+
) {
250+
this.handleWithTemplateContext(context, storageName, prefix, templateName, (template, storage) -> {
251+
var status = storage.deleteDirectory(template, path);
252+
this.ok(context)
253+
.body(status ? this.success().toString() : this.failure().toString())
254+
.context()
255+
.closeAfter(true)
256+
.cancelNext(true);
257+
});
258+
}
259+
241260
@BearerAuth
242261
@HttpRequestHandler(paths = "/api/v2/template/{storage}/{prefix}/{name}", methods = "DELETE")
243262
private void handleTemplateDeleteRequest(

modules/storage-s3/src/main/java/eu/cloudnetservice/modules/s3/S3TemplateStorage.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,29 @@ public boolean deleteFile(@NonNull ServiceTemplate template, @NonNull String pat
349349
}
350350
}
351351

352+
@Override
353+
public boolean deleteDirectory(@NonNull ServiceTemplate template, @NonNull String path) {
354+
try {
355+
// get the contents we want to delete
356+
Set<ObjectIdentifier> toDelete = new HashSet<>();
357+
this.listAllObjects(
358+
this.getBucketPath(template, path),
359+
null,
360+
object -> toDelete.add(ObjectIdentifier.builder().key(object.key()).build()));
361+
362+
// build the delete request
363+
var deleteRequest = DeleteObjectsRequest.builder()
364+
.bucket(this.config().bucket())
365+
.delete(Delete.builder().quiet(true).objects(toDelete).build())
366+
.build();
367+
this.client.deleteObjects(deleteRequest);
368+
// success
369+
return true;
370+
} catch (Exception exception) {
371+
return false;
372+
}
373+
}
374+
352375
@Override
353376
public @Nullable InputStream newInputStream(@NonNull ServiceTemplate template, @NonNull String path) {
354377
try {

modules/storage-sftp/src/main/java/eu/cloudnetservice/modules/sftp/SFTPTemplateStorage.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,15 @@ public boolean deleteFile(@NonNull ServiceTemplate template, @NonNull String pat
273273
}, false);
274274
}
275275

276+
@Override
277+
public boolean deleteDirectory(@NonNull ServiceTemplate template, @NonNull String path) {
278+
return this.executeWithClient(client -> {
279+
this.deleteDir(client, this.constructRemotePath(template, path));
280+
client.rmdir(this.constructRemotePath(template, path));
281+
return true;
282+
}, false);
283+
}
284+
276285
@Override
277286
public @Nullable InputStream newInputStream(@NonNull ServiceTemplate st, @NonNull String path) throws IOException {
278287
var client = this.pool.takeClient();

node/src/main/java/eu/cloudnetservice/node/template/LocalTemplateStorage.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,17 @@ public boolean deleteFile(@NonNull ServiceTemplate template, @NonNull String pat
193193
return false;
194194
}
195195

196+
@Override
197+
public boolean deleteDirectory(@NonNull ServiceTemplate template, @NonNull String path) {
198+
var dirPath = this.getTemplatePath(template).resolve(path);
199+
if (Files.exists(dirPath) && Files.isDirectory(dirPath)) {
200+
FileUtil.delete(dirPath);
201+
return true;
202+
}
203+
204+
return false;
205+
}
206+
196207
@Override
197208
public @Nullable InputStream newInputStream(
198209
@NonNull ServiceTemplate template,

0 commit comments

Comments
 (0)