Skip to content

Instantly share code, notes, and snippets.

@axeda
Created August 8, 2013 14:42
Show Gist options
  • Select an option

  • Save axeda/6185190 to your computer and use it in GitHub Desktop.

Select an option

Save axeda/6185190 to your computer and use it in GitHub Desktop.
Check for an image file extension. If it has one, scale it to a smaller version and save it to the file store.
import com.axeda.drm.sdk.Context
import com.axeda.drm.sdk.data.*
import com.axeda.drm.sdk.device.*
import com.axeda.drm.sdk.mobilelocation.CurrentMobileLocationFinder
import com.axeda.drm.sdk.mobilelocation.MobileLocation
import com.axeda.drm.sdk.mobilelocation.MobileLocationFinder
import com.axeda.sdk.v2.bridge.FileInfoBridge
import static com.axeda.sdk.v2.dsl.Bridges.*
import com.axeda.services.v2.ExecutionResult
import com.axeda.services.v2.FileInfo
import com.axeda.services.v2.FileInfoReference
import com.axeda.services.v2.FileUploadSession
import net.sf.json.JSONObject
import groovy.json.JsonBuilder
import net.sf.json.JSONArray
import com.axeda.drm.sdk.scripto.Request
import org.apache.commons.io.IOUtils
import org.apache.commons.lang.exception.ExceptionUtils
import com.axeda.common.sdk.id.Identifier
import groovy.json.*
import javax.imageio.ImageIO;
import java.awt.RenderingHints
import java.awt.image.BufferedImage
import java.io.ByteArrayOutputStream;
import java.awt.*
import java.awt.geom.*
import javax.imageio.*
import java.awt.image.*
import java.awt.Graphics2D
import javax.imageio.stream.ImageInputStream
/*
Image-specific FileStore entry point to post and store files
*/
def contentType = "application/json"
final def serviceName = "StoreScaledImage"
// Create a JSON Builder
def json = new JsonBuilder()
// Global try/catch. Gotta have it, you never know when your code will be exceptional!
try {
Context CONTEXT = Context.getSDKContext()
def filesList = []
def datestring = new Date().time
InputStream inputStream = Request.inputStream
def reqbody = Request.body
// all of our Request Parameters are available here
def params = Request.parameters
def filename = Request?.headers?.'Content-Disposition' ?
Request?.headers?.'Content-Disposition' : "file___" + datestring + ".txt"
def filelabel = Request.parameters.filelabel ?: filename
def description = Request.parameters.description ?: filename
def contType = Request.headers?."content-type" ?: "image/jpeg"
def tag = Request.parameters.tag ?: "cappimg"
def encoded = Request.parameters.encoded?.toBoolean()
def dimlimit = params.dimlimit ? params.dimlimit : 280
// host is available in the headers when the script is called with AJAX
def domain = Request.headers?.host
byte[] bytes = IOUtils.toByteArray(inputStream);
def fileext = filename.substring(filename.indexOf(".") + 1,filename.size())
def outerMap = [:]
// check that file extension matches an image type
if (fileext ==~ /([^\s]+(\.(?i)(jpg|jpeg|png|gif|bmp))$)/){
if (inputStream.available() > 0) {
def scaledImg
try {
def img = ImageIO.read(inputStream)
def width = img?.width
def height = img?.height
def ratio = 1.0
def newBytes
if (img){
if (width > dimlimit || height > dimlimit){
// shrink by the smaller side so it can still be over the limit
def dimtochange = width > height ? height : width
ratio = dimlimit / dimtochange
width = Math.floor(width * ratio).toInteger()
height = Math.floor(height * ratio).toInteger()
}
newBytes = doScale(img, width, height, ratio, fileext)
if (newBytes?.size() > 0){
bytes = newBytes
}
}
}
catch(Exception e){
logger.info(e.localizedMessage)
}
outerMap.byteCount = bytes.size()
FileInfoBridge fib = fileInfoBridge
FileInfo myImageFile = new FileInfo(filelabel: filelabel,
filename: filename,
filesize: bytes?.size(),
description: description,
tags: tag
)
myImageFile.contentType = contType
FileUploadSession fus = new FileUploadSession();
fus.files = [myImageFile]
ExecutionResult fer = fileUploadSessionBridge.create(fus);
myImageFile.sessionId = fer.succeeded.getAt(0)?.id
ExecutionResult fileInfoResult = fib.create(myImageFile)
if (fileInfoResult.successful) {
outerMap.fileInfoSave = "File Info Saved"
outerMap.sessionId = "File Upload SessionID: "+fer.succeeded.getAt(0)?.id
outerMap.fileInfoId = "FileInfo ID: "+fileInfoResult?.succeeded.getAt(0)?.id
ExecutionResult er = fib.saveOrUpdate(fileInfoResult.succeeded.getAt(0).id,new ByteArrayInputStream(bytes))
def fileInfoId = fileInfoResult?.succeeded.getAt(0)?.id
String url = "${domain}/services/v1/rest/Scripto/execute/DownloadFile?fileId=${fileInfoId}"
if (er.successful) {
outerMap.url = url
} else {
outerMap.save = "false"
logger.info(logFailure(er,outerMap))
}
} else {
logger.info(logFailure(fileInfoResult, outerMap))
}
} else {
outerMap.bytesAvail = "No bytes found to upload"
}
} else {
outerMap.imagetype = "Extension $fileext is not a supported image file type."
}
filesList << outerMap
// return the JSONBuilder contents
// we specify the content type, and any object as the return (even an outputstream!)
return ["Content-Type": contentType,"Content":JSONArray.fromObject(filesList).toString(2)]
// alternately you may just want to serial an Object as JSON:
// return ["Content-Type": contentType,"Content":JSONArray.fromObject(invertedMessages).toString(2)]
} catch (Exception e) {
// I knew you were exceptional!
// we'll capture the output of the stack trace and return it in JSON
json.Exception(
description: "Execution Failed!!! An Exception was caught...",
stack: ExceptionUtils.getFullStackTrace(e)
)
// return the output
return ["Content-Type": contentType, "Content": json.toPrettyString()]
}
def doScale(image, width, height, ratio, fileext){
if (image){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
def bytes
def scaledImg = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB )
Graphics2D g = scaledImg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.scale(ratio,ratio)
g.drawImage(image, null, null);
g.dispose();
ImageIO.write( scaledImg, fileext, baos )
baos.flush()
bytes = baos.toByteArray()
baos.close()
}
else {
logger.info("image to be scaled is null")
return false
}
return bytes
}
private void logFailure(ExecutionResult fileInfoResult, LinkedHashMap outerMap) {
outerMap.message = fileInfoResult.failures.getAt(0)?.message
outerMap.source = fileInfoResult.failures.getAt(0)?.sourceOfFailure
outerMap.details = fileInfoResult.failures.getAt(0)?.details?.toString()
outerMap.fileInfoSave = "false"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment