-
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
5 changed files
with
194 additions
and
141 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
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 | ||
} |
This file was deleted.
Oops, something went wrong.
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,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 | ||
} |
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
Binary file not shown.