Beispiel #1
0
class Epub:
    def __init__(self):
        pass

    def __activate__(self, context):
        self.velocityContext = context

        oid = self.vc("formData").get("oid")
        print "--- Creating ePub for: %s ---" % oid
        try:
            self.__epubMimetypeStream = None
            self.__epubContainerStream = None
            self.__epubcss = None
            self.__orderedItem = []
            self.__itemRefDict = {}
            # get the package manifest
            object = Services.getStorage().getObject(oid)
            sourceId = object.getSourceId()
            payload = object.getPayload(sourceId)
            self.__manifest = JsonSimple(payload.open())
            payload.close()
            object.close()
            # create the epub
            self.__getDigitalItems(self.__manifest.getJsonSimpleMap("manifest"))
            self.__createEpub()
        except Exception, e:
            log.error("Failed to create epub", e)
            self.vc("response").setStatus(500)
            writer = self.vc("response").getPrintWriter("text/plain; charset=UTF-8")
            writer.println(str(e))
            writer.close()
 def __mergeData(self):
     
     requiredMetadata = ["format", "width", "height", "timeSpent", "size"]
     
     try:
         # ffmpeg.info data:
         ffmpegPayload = self.__object.getPayload("ffmpeg.info")
         ffmpegInfo = JsonSimple(ffmpegPayload.open())
         
         ffmpegOutput = ffmpegInfo.getJsonSimpleMap(["outputs"])
         
         map = JsonSimple()
         for key in ffmpegOutput.keySet():
             data = ffmpegOutput.get(key)
             detailMap = JsonSimple()
             for field in data.getJsonObject().keySet():
                 if field in requiredMetadata:
                     detailMap.getJsonObject().put(field, data.getString("", [field]))
             map.getJsonObject().put(key, detailMap)
         
         self.__metadata.getJsonObject().put("outputs", map)
         ffmpegPayload.close()
     except Exception, e:
         print str(e)
Beispiel #3
0
class DetailData:
    def __init__(self):
        pass
    
    def __activate__(self, context):
        self.__ffmpegRaw = None
        self.__ffmpegData = None
        self.__ffmpegOutputs = None
        self.__urlBase = context["urlBase"]
    
    def escape(self, text):
        return StringEscapeUtils.escapeHtml(text)

    def getBasicFFmpegData(self, index):
        if self.__ffmpegData is not None:
            output = self.__ffmpegData.getString(None, [index])
            if output is not None:
                return output
        return ""

    def getFFmpegData(self, pid, index):
        if self.__ffmpegOutputs is not None:
            output = self.__ffmpegOutputs.get(pid).getString(None, [index])
            if output is not None:
                return output
        return ""

    def getFFmpegDebugging(self, pid):
        if self.__ffmpegOutputs is not None:
            output = self.__ffmpegOutputs.get(pid).getString(None, ["debugOutput"])
            return self.makeHtml(output)
        else:
            return "Not found!"

    # Get The MIME Type of a payload
    def getMimeType(self, pid, parent):
        if parent is not None:
            object = parent.getObject()
            if object is not None:
                try:
                    payload = object.getPayload(pid)
                    return payload.getContentType()
                except:
                    pass
        return "unknown"

    def getRawFFmpeg(self):
        return self.makeHtml(self.__ffmpegRaw)

    def getSplashScreen(self, metadata, preference):
        #TODO - Proper checking that the prefered payload actually exists
        if preference is not None and preference != "":
            return preference

        # Fall back to the thumbnail if no preference was given
        thumbnail = metadata.get("thumbnail")
        if thumbnail is not None:
            return thumbnail
        return ""

    def getTranscodings(self):
        if self.__ffmpegOutputs is not None:
            return self.__ffmpegOutputs.keySet()
        else:
            return ArrayList()

    def isAudio(self, mime):
        return mime.startswith("audio/")

    def isVideo(self, mime):
        return mime.startswith("video/")

    # Turn a Python boolean into a javascript boolean
    def jsBool(self, pBool):
        if pBool:
            return "true"
        else:
            return "false"

    def makeHtml(self, input):
        input = input.replace(" ", " ")
        input = input.replace("\r\n", "<br/>")
        input = input.replace("\r", "<br/>")
        input = input.replace("\n", "<br/>")
        input = input.replace("\\r\\n", "<br/>")
        input = input.replace("\\r", "<br/>")
        input = input.replace("\\n", "<br/>")
        return input

    def niceSize(self, size):
        # Cast to a number
        size = eval(size)
        # Bytes
        if (size > 1000):
            # KBytes
            size = size / 1000;
            if (size > 1000):
                # MBytes
                size = size / 1000;
                if (size > 1000):
                    # GBytes
                    size = size / 1000;
                    return str(round(size, 1)) + "Gb"
                else:
                    return str(round(size, 1)) + "Mb"
            else:
                return str(round(size, 1)) + "Kb"
        else:
            return str(size) + "b"

    def parseFFmpeg(self, parent):
        if parent is not None:
            object = parent.getObject()
            if object is not None:
                payload = None
                try:
                    payload = object.getPayload("ffmpeg.info")
                    # Stream the content out to string
                    out = ByteArrayOutputStream()
                    IOUtils.copy(payload.open(), out)
                    payload.close()
                    self.__ffmpegRaw = out.toString("UTF-8")
                    out.close()
                    payload.close()
                    # And parse it
                    self.__ffmpegData = JsonSimple(self.__ffmpegRaw)
                    if self.__ffmpegData is None:
                        return False
                    else:
                        self.__ffmpegOutputs = self.__ffmpegData.getJsonSimpleMap(["outputs"])
                        return True
                except:
                    if payload is not None:
                        payload.close()
        return False
    
    def getUrlBase(self):
        return self.__urlBase
Beispiel #4
0
class HeadData:
    def __init__(self):
        pass

    def __activate__(self, context):
        self.velocityContext = context
        self.__metadata = self.vc("metadata")
        self.__urlBase = None
        self.__ffmpegRaw = None
        self.__ffmpegData = None
        self.__ffmpegOutputs = None

    # Get from velocity context
    def vc(self, index):
        if self.velocityContext[index] is not None:
            return self.velocityContext[index]
        else:
            log.error("ERROR: Requested context entry '" + index + "' doesn't exist")
            return None

    def getBasicFFmpegData(self, index):
        if self.__ffmpegData is not None:
            output = self.__ffmpegData.getString(None, [index])
            if output is not None:
                return output
        return ""

    def getFFmpegData(self, pid, index):
        if self.__ffmpegOutputs is not None:
            output = self.__ffmpegOutputs.get(pid).getString(None, [index])
            if output is not None:
                return output
        return ""

    # Get The MIME Type of a payload
    def getMimeType(self, pid, parent):
        if parent is not None:
            object = parent.getObject()
            if object is not None:
                try:
                    payload = object.getPayload(pid)
                    return payload.getContentType()
                except:
                    pass
        return "unknown"

    def getTranscodings(self):
        pass

    def getRawFFmpeg(self):
        return self.__ffmpegRaw

    def getSplashScreen(self, metadata, preference):
        #TODO - Proper checking that the prefered payload actually exists
        if preference is not None and preference != "":
            return preference

        # Fall back to the thumbnail if no preference was given
        thumbnail = metadata.getFirst("thumbnail")
        if thumbnail is not None:
            return thumbnail
        return ""

    def isAudio(self, mime):
        return mime.startswith("audio/")

    def isVideo(self, mime):
        return mime.startswith("video/")

    # Turn a Python boolean into a javascript boolean
    def jsBool(self, pBool):
        if pBool:
            return "true"
        else:
            return "false"

    def parseFFmpeg(self, parent):
        if parent is not None:
            payload = None
            object = parent.getObject()
            if object is not None:
                try:
                    payload = object.getPayload("ffmpeg.info")
                    # Stream the content out to string
                    out = ByteArrayOutputStream()
                    IOUtils.copy(payload.open(), out)
                    payload.close()
                    self.__ffmpegRaw = out.toString("UTF-8")
                    out.close()
                    payload.close()
                    # And parse it
                    self.__ffmpegData = JsonSimple(self.__ffmpegRaw)
                    if self.__ffmpegData is None:
                        return False
                    else:
                        self.__ffmpegOutputs = self.__ffmpegData.getJsonSimpleMap(["outputs"])
                        return True
                except:
                    if payload is not None:
                        payload.close()
        return False

    def renderUrl(self, payload):
        if self.__urlBase is None:
            portal = self.vc("portalPath")
            page = self.vc("pageName")
            id = self.__metadata.getFirst("id")
            self.__urlBase = portal + "/" + page + "/" + id + "/"
        return self.__urlBase + payload