Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ot.delete template #175

Merged
merged 8 commits into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ public interface IProtectedEmailerProcessor {

/** Loads an HTML email template from cloud storage. */
LoadTemplateResponse loadTemplate(JWTData userData, String templateName);

/** Deletes an HTML email template with the given name in cloud storage. */
void deleteTemplate(JWTData userData, String templateName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public Router initializeRouter(Vertx vertx) {

registerAddTemplate(router);
registerLoadTemplate(router);
registerDeleteTemplate(router);

return router;
}
Expand All @@ -46,7 +47,7 @@ private void handleAddTemplate(RoutingContext ctx) {

end(ctx.response(), 200);
}

private void registerLoadTemplate(Router router) {
Route loadTemplate = router.get("/load_template/:template_name");
loadTemplate.handler(this::handleLoadTemplate);
Expand All @@ -60,4 +61,18 @@ private void handleLoadTemplate(RoutingContext ctx) {

end(ctx.response(), 200, JsonObject.mapFrom(loadTemplateResponse).toString());
}

private void registerDeleteTemplate(Router router) {
Route deleteTemplate = router.delete("/delete_template/:template_name");
deleteTemplate.handler(this::handleDeleteTemplate);
}

private void handleDeleteTemplate(RoutingContext ctx) {
JWTData userData = ctx.get("jwt_data");
String templateName = RestFunctions.getRequestParameterAsString(ctx.request(), "template_name");

processor.deleteTemplate(userData, templateName);

end(ctx.response(), 200);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@
import com.codeforcommunity.auth.JWTData;
import com.codeforcommunity.dto.emailer.AddTemplateRequest;
import com.codeforcommunity.dto.emailer.LoadTemplateResponse;
import com.codeforcommunity.dto.leaderboard.LeaderboardEntry;
import com.codeforcommunity.enums.PrivilegeLevel;
import com.codeforcommunity.enums.ReservationAction;
import com.codeforcommunity.exceptions.UserDoesNotExistException;
import com.codeforcommunity.requester.S3Requester;

import org.jooq.DSLContext;

import java.util.List;

import static org.jooq.impl.DSL.count;
import static org.jooq.generated.tables.Users.USERS;
import org.jooq.generated.tables.records.UsersRecord;
Expand Down Expand Up @@ -58,4 +53,10 @@ public LoadTemplateResponse loadTemplate(JWTData userData, String templateName)

return loadTemplateResponse;
}

@Override
public void deleteTemplate(JWTData userData, String templateName) {
assertAdminOrSuperAdmin(userData.getPrivilegeLevel());
S3Requester.deleteHTML(templateName, TEMPLATE_DIR);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.codeforcommunity.requester;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.DeleteObjectRequest;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
Expand All @@ -15,10 +17,10 @@
import com.codeforcommunity.dto.emailer.LoadTemplateResponse;
import com.codeforcommunity.exceptions.BadRequestHTMLException;
import com.codeforcommunity.exceptions.BadRequestImageException;
import com.codeforcommunity.exceptions.InvalidURLException;
import com.codeforcommunity.exceptions.S3FailedUploadException;
import com.codeforcommunity.exceptions.InvalidURLException;
import com.codeforcommunity.propertiesLoader.PropertiesLoader;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -215,8 +217,8 @@ public static String getFileNameWithoutExtension(String eventTitle, String suffi
}

/**
* Validate the given string encoding of HTML and upload it to the user upload S3 bucket.
* HTML will be overwritten in S3 if another file of the same name is uploaded.
* Validate the given string encoding of HTML and upload it to the user upload S3 bucket. HTML
* will be overwritten in S3 if another file of the same name is uploaded.
*
* @param name the desired name of the new file in S3 (without a file extension).
* @param directoryName the desired directory of the file in S3 (without leading or trailing '/').
Expand Down Expand Up @@ -282,7 +284,7 @@ public static String uploadHTML(

return String.format("%s/%s/%s", externs.getBucketPublicUrl(), directoryName, name);
}

// helper to check whether the given path exists
public static boolean pathExists(String path) {
return externs.getS3Client().doesObjectExist(externs.getBucketPublic(), path);
Expand All @@ -305,7 +307,7 @@ public static LoadTemplateResponse loadHTML(String name, String directoryName) {
throw new InvalidURLException();
}

// Create the request to delete the HTML
// Create the request to get the HTML
GetObjectRequest awsRequest = new GetObjectRequest(externs.getBucketPublic(), htmlPath);

S3Object HTMLFile = externs.getS3Client().getObject(awsRequest);
Expand All @@ -315,7 +317,7 @@ public static LoadTemplateResponse loadHTML(String name, String directoryName) {
while ((c = HTMLFile.getObjectContent().read()) != -1) {
content.append((char) c);
}
} catch(IOException e) {
} catch (IOException e) {
throw new BadRequestHTMLException("HTML file could not be decoded to string");
}

Expand All @@ -325,4 +327,25 @@ public static LoadTemplateResponse loadHTML(String name, String directoryName) {

return new LoadTemplateResponse(HTMLContent, HTMLFile.getKey(), htmlAuthor);
}

/**
* Delete the existing HTML file with the given name from the user uploads S3 bucket.
*
* @param name the name of the HTML file in S3 to be deleted.
* @param directoryName the directory of the file in S3 (without leading or trailing '/').
* @throws InvalidURLException if the file does not exist.
* @throws SdkClientException if the deletion from S3 failed.
*/
public static void deleteHTML(String name, String directoryName) {
String htmlPath = directoryName + "/" + name + "_template.html";

if (!pathExists(htmlPath)) {
throw new InvalidURLException();
}

// Create the request to delete the HTML
DeleteObjectRequest awsRequest = new DeleteObjectRequest(externs.getBucketPublic(), htmlPath);

externs.getS3Client().deleteObject(awsRequest);
}
}