Skip to content

Commit

Permalink
Better hosting
Browse files Browse the repository at this point in the history
  • Loading branch information
j3lte committed May 14, 2024
1 parent 33971af commit 3df6571
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 141 deletions.
24 changes: 24 additions & 0 deletions src/javascriptsource/imagehosting/actions/GetID.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
import "mx-global";
import { Big } from "big.js";

// BEGIN EXTRA CODE
// END EXTRA CODE

/**
* Get Object ID of a Mendix Object
* @param {MxObject} obj
* @returns {Promise.<string>}
*/
export async function GetID(obj) {
// BEGIN USER CODE
const guid = obj.getGuid();
return `${guid}`;
// END USER CODE
}
140 changes: 0 additions & 140 deletions src/javasource/imagehosting/actions/HostImage.java

This file was deleted.

168 changes: 168 additions & 0 deletions src/javasource/imagehosting/actions/HostImageByID.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à, etc. are supported in comments.

package imagehosting.actions;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import com.mendix.core.Core;
import com.mendix.datastorage.XPathBasicQuery;
import com.mendix.externalinterface.connector.RequestHandler;
import com.mendix.m2ee.api.IMxRuntimeRequest;
import com.mendix.m2ee.api.IMxRuntimeResponse;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import com.mendix.webui.CustomJavaAction;

public class HostImageByID extends CustomJavaAction<java.lang.Boolean>
{
private final java.lang.String RequestHandlerPath;
private final java.lang.String SearchEntity;

public HostImageByID(
IContext context,
java.lang.String _requestHandlerPath,
java.lang.String _searchEntity
)
{
super(context);
this.RequestHandlerPath = _requestHandlerPath;
this.SearchEntity = _searchEntity;
}

@java.lang.Override
public java.lang.Boolean executeAction() throws Exception
{
// BEGIN USER CODE
String path = RequestHandlerPath;
if (!path.endsWith("/")) {
path += "/";
}

try {
Core.addRequestHandler(path, new requestHandler()); //initialize the request handler
Core.getLogger("RequestHandlersByID").info(path + " has been initialized");

return true;
} catch (Exception e) {
Core.getLogger("RequestHandlersByID").error(path + " failed to initialize");
return false;
}
// END USER CODE
}

/**
* Returns a string representation of this action
* @return a string representation of this action
*/
@java.lang.Override
public java.lang.String toString()
{
return "HostImageByID";
}

// BEGIN EXTRA CODE
class requestHandler extends RequestHandler {
@Override
public void processRequest(IMxRuntimeRequest request,
IMxRuntimeResponse response, String arg2) throws Exception
{

String requestPath = request.getHttpServletRequest().getRequestURI();
String path = RequestHandlerPath;
if (!path.endsWith("/")) {
path += "/";
}
String fileName = requestPath.replace(path, "");
if (fileName.startsWith("/")) {
fileName = fileName.substring(1);
}

int extensionIndex = fileName.lastIndexOf(".");

if (extensionIndex == -1) {
Core.getLogger("RequestHandlersByID").error("Not a valid name");
response.setStatus(IMxRuntimeResponse.BAD_REQUEST);
return;
}

String extension = fileName.substring(extensionIndex + 1);
fileName = fileName.substring(0, extensionIndex);

if (!fileName.matches("\\d+")) {
Core.getLogger("RequestHandlersByID").error("Not a valid name");
response.setStatus(IMxRuntimeResponse.BAD_REQUEST);
return;
}

File file = new File(fileName + "." + extension);
String mimeType = URLConnection.guessContentTypeFromName(file.getName());

try {
Core.getLogger("RequestHandlersByID").debug("Request Param found: " + fileName);
Long objectId = Long.parseLong(fileName);

XPathBasicQuery query = Core
.createXPathQuery("//" + SearchEntity + "[id=$value]")
.setVariable("value", objectId)
.setAmount(1);

Core.getLogger("Image Hosting").debug(query.toString());
java.util.List<IMendixObject> result = query.execute(getContext());

if (result.size() == 0) {
Core.getLogger("RequestHandlersByID").error("Not found: " + file.getName());
response.setStatus(IMxRuntimeResponse.NOT_FOUND);
return;
}

IMendixObject obj = result.get(0);

OutputStream os = response.getOutputStream(); //take the output stream from the IMxRuntimeResponse
InputStream is = Core.getFileDocumentContent(getContext(), obj); //get an input stream from your mendix object

response.setContentType(mimeType);
response.addHeader("Content-Disposition", "inline");

try {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
Date changedDate = obj.getChangedDate(getContext());
response.addHeader("Last-Modified", dateFormat.format(changedDate));
} catch (Exception _e) {}

response.setStatus(IMxRuntimeResponse.OK);
is.transferTo(os);
} catch (Exception e) {
Core.getLogger("RequestHandlersByID").error( e);
response.setStatus(IMxRuntimeResponse.BAD_REQUEST); //if this fails, respond with 200 status anyways
}
}
}

public static String determineContentType (String fileName) {
try {
String file_extension = fileName.substring(fileName.lastIndexOf(".") + 1);
return file_extension;
} catch (Exception e) {
Core.getLogger("Image Hosting").error( "failed to determine file extension");
return null;
}

}

// END EXTRA CODE
}
3 changes: 2 additions & 1 deletion src/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
<module name="ImageHosting" />
<projectFile path="project.mpr" />
<files>
<file path="javasource/imagehosting/actions/HostImage.java" />
<file path="javasource/imagehosting/actions/HostImageByID.java" />
<file path="javascriptsource/imagehosting/actions/GetOrigin.js" />
<file path="javascriptsource/imagehosting/actions/GetID.js" />
<file path="themesource/imagehosting/settings.json" />
<file path="themesource/imagehosting/native/design-properties.json" />
<file path="themesource/imagehosting/native/main.js" />
Expand Down
Binary file modified src/project.mpr
Binary file not shown.

0 comments on commit 3df6571

Please sign in to comment.