hackedteam/rcs-console

View on GitHub
src/it/ht/rcs/console/operations/view/evidences/advanced/viewers/File.mxml

Summary

Maintainability
Test Coverage
<?xml version="1.0" encoding="utf-8"?>
<s:BorderContainer xmlns:fx="http://ns.adobe.com/mxml/2009"
                                     xmlns:s="library://ns.adobe.com/flex/spark"
                                     xmlns:mx="library://ns.adobe.com/flex/mx"
                                     height="100%"
                                     width="100%"
                                     borderColor="0xCCCCCC"
                                     backgroundColor="0xFFFFFF"
                                     backgroundAlpha="1"
                                     creationComplete="init()">
    <fx:Script>
        <![CDATA[
            import flash.filesystem.File;

            import flashx.textLayout.conversion.TextConverter;

            import it.ht.rcs.console.DB;
            import it.ht.rcs.console.evidence.controller.EvidenceManager;
            import it.ht.rcs.console.evidence.model.Evidence;
            import it.ht.rcs.console.operations.view.evidences.EvidenceAdvancedViewer;
            import it.ht.rcs.console.operations.view.evidences.EvidencesView;
            import it.ht.rcs.console.utils.FileUtils;
            import it.ht.rcs.console.utils.Size;
            import it.ht.rcs.console.utils.StringUtils;
            import it.ht.rcs.console.monitor.controller.LicenseManager;

            import mx.rpc.events.ResultEvent;
            import mx.rpc.events.FaultEvent;

            import locale.R;

            private var _evidence:Object;
            private var file:flash.filesystem.File;
            private var stream:URLStream;
            private var extension:String;
            private var request:URLRequest
            private var loader:URLLoader;

            private var mainView:EvidencesView;
            private var t:uint;

            [Bindable]
            private var size:String;

            [Bindable]
            private var accessMask:String;

            [Bindable]
            private var translation:String;

            [Bindable]
            private var content:String;

            [Bindable]
            private var contentLoaded:Boolean;

            [Bindable]
            public var idVisible:Boolean;

            [Bindable]
            public function set evidence(value:Object):void
            {
                _evidence=value;

                t=setTimeout(updateView, 100)
            }

            private function updateView():void
            {
                if (_evidence)
                {

                    accessMask=FileUtils.getAccessMask(_evidence.data.access);
                    if (isNaN(evidence.data.size))
                    {
                        size="(unknown)"
                    }
                    else
                    {
                        size=Size.toHumanBytes(evidence.data.size)
                    }
                    downloadStatusTxt.text=""
                    downloadBtn.label="Download"

                    translation=_evidence.data.tr;
                    translationTxt.setStyle("color", 0x666666)
                    var id:String=encodeURIComponent(evidence._id);
                    var target:String=encodeURIComponent(EvidenceManager.instance.evidenceFilter.target);
                    var url:String=DB.hostAutocomplete(Console.currentSession.server) + "evidence/body/" + id + "?target=" + target;
                    loader=new URLLoader()
                    loader.addEventListener(Event.COMPLETE, onContentLoaded);
                    loader.load(new URLRequest(url));
                    callLater(onTranslate, [null])
                    clearTimeout(t)
                }
            }

            private function init():void
            {
                var container:HGroup=this.owner as HGroup;
                var advancedViewer:EvidenceAdvancedViewer=container.owner as EvidenceAdvancedViewer;
                mainView=advancedViewer.owner as EvidencesView;
                mainView.addEventListener("translate", onTranslate);
            }

            private function onContentLoaded(e:Event):void
            {
                loader.removeEventListener(Event.COMPLETE, onContentLoaded);
                content=e.currentTarget.data;
                if (content == "" || content == null)
                    content=R.get('OCR_NO_DATA');

                contentTxt.textFlow=TextConverter.importToFlow('<b>Content: </b>' + content, TextConverter.TEXT_FIELD_HTML_FORMAT)

            }

            public function get evidence():Object
            {
                return _evidence; //program, path, size
            }

            private function lineBreak(s:String):String
            {
                var result:String=s.replace(new RegExp("\n", "g"), "<br>");
                return result;
            }

            private function onTranslate(e:Event):void
            {
                if (mainView.translation)
                {
                    //Show translated OCR content
                    switch (evidence.data.tr)
                    {
                        case 'TRANS_QUEUED':
                            translation='(' + R.get('TRANS_QUEUED') + ')';
                            break;
                        case 'TRANS_IN_PROGRESS':
                            translation='(' + R.get('TRANS_IN_PROGRESS') + ')';
                            break;
                        case 'TRANS_ERROR':
                            translation='(' + R.get('TRANS_ERROR') + ')';
                            break;
                        case null:
                            translation='(' + R.get('TRANSLATION_UNAVAILABLE') + ')';
                            break;
                        default:
                            translation=evidence.data.tr;
                    }
                    translationTxt.visible=translationTxt.includeInLayout=true;
                    contentTxt.visible=contentTxt.includeInLayout=false;

                    if (mainView.translation && LicenseManager.instance.translation)
                    {

                        if (evidence.data.tr == null || evidence.data.tr == "")
                        {
                            //var id:String=encodeURIComponent(evidence._id);
                            //var target:String=encodeURIComponent(EvidenceManager.instance.evidenceFilter.target);
                            // EvidenceManager.instance.translate(id, target, onTranslateResult, onTranslateFault)
                        }
                    }
                }
                else
                {
                    translationTxt.visible=translationTxt.includeInLayout=false;
                    contentTxt.visible=contentTxt.includeInLayout=true;
                }
            }

            private function onTranslateResult(e:ResultEvent):void
            {

                evidence=e.result;
                for (var i:int=0; i < EvidenceManager.instance._view.length; i++)
                {
                    var current:Evidence=EvidenceManager.instance._view.getItemAt(i) as Evidence;
                    if (current)
                    {
                        if (current._id == e.result._id)
                        {
                            current.data.tr=e.result.data.tr;
                        }
                    }
                }

            }

            private function onTranslateFault(e:FaultEvent):void
            {
                trace("translation fault")
            }

            private function getFile():void
            {
                if (downloadBtn.label == "Download")
                {
                    var target:String=EvidenceManager.instance.evidenceFilter.target;
                    var url:String
                    if (DB.instance.demo)
                    {
                        url="grid/" + evidence.data._grid
                    }
                    else
                    {
                        url=DB.hostAutocomplete(Console.currentSession.server) + "grid/" + evidence.data._grid + "?target_id=" + encodeURIComponent(target);
                    }

                    extension=StringUtils.getExtension(evidence.data.path)
                    var fileName:String=evidence.data._grid + encodeURIComponent(target) + "." + extension; // ?? get original filename? (StringUtils.getFilename)

                    request=new URLRequest(url);
                    stream=new URLStream();
                    file=flash.filesystem.File.documentsDirectory.resolvePath(fileName);
                    file.browseForSave("Download " + fileName);
                    file.addEventListener(Event.SELECT, onSelect);
                }
                else if (downloadBtn.label == "Open")
                {
                    try
                    {
                        file.openWithDefaultApplication()
                    }
                    catch (e:Error)
                    {
                        downloadStatusTxt.text="Unable to open the file with default application."
                    }

                }
            }

            private function onSelect(e:Event):void
            {
                downloadBtn.enabled=false;
                if (file.extension != extension)
                {
                    file=new flash.filesystem.File(file.nativePath + "." + extension);
                }
                stream.addEventListener(Event.COMPLETE, onFileDownloaded);
                stream.addEventListener(ProgressEvent.PROGRESS, onDownloadProgress);
                stream.addEventListener(IOErrorEvent.IO_ERROR, onDownloadError);
                stream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onDownloadError)
                stream.load(request);
            }

            private function onDownloadProgress(e:ProgressEvent):void
            {
                var perc:int=int((e.bytesLoaded / e.bytesTotal) * 100)
                downloadStatusTxt.text="Downloading file..." + perc + " % completed."
            }

            private function onDownloadError(e:*):void
            {
                downloadBtn.enabled=true;
                downloadStatusTxt.text="Error downloading file. Try again"
            }

            private function onFileDownloaded(e:Event):void
            {
                downloadBtn.label="Open"
                downloadStatusTxt.text="File downloaded successfully."
                downloadBtn.enabled=true;
                var fileData:ByteArray=new ByteArray();
                stream.readBytes(fileData, 0, stream.bytesAvailable);
                var fileStream:FileStream=new FileStream();
                fileStream.open(file, FileMode.WRITE);
                fileStream.writeBytes(fileData, 0, fileData.length);
                fileStream.close();
            }
        ]]>
    </fx:Script>
    <s:layout>
        <s:VerticalLayout paddingBottom="10"
                                            paddingLeft="10"
                                            paddingRight="10"
                                            paddingTop="10"/>
    </s:layout>
    <s:RichText id="idTxt"
                            width="100%"
                            textFlow="{TextConverter.importToFlow('&lt;b&gt;Id: &lt;/b&gt;'+evidence._id, TextConverter.TEXT_FIELD_HTML_FORMAT)}"
                            fontSize="12"
                            visible="{idVisible}"
                            includeInLayout="{idVisible}"/>
    <s:Line width="100%"
                    visible="{idVisible}"
                    includeInLayout="{idVisible}">
        <s:stroke>
            <s:SolidColorStroke color="0xCCCCCC"/>
        </s:stroke>
    </s:Line>
    <s:RichText id="programTxt"
                            width="100%"
                            textFlow="{TextConverter.importToFlow('&lt;b&gt;'+R.get('PROGRAM')+': &lt;/b&gt;'+evidence.data.program, TextConverter.TEXT_FIELD_HTML_FORMAT)}"
                            fontSize="12"
                            visible="{evidence.data.type=='open'}"
                            includeInLayout="{evidence.data.type=='open'}"/>
    <s:Line width="100%"
                    visible="{evidence.data.type=='open'}"
                    includeInLayout="{evidence.data.type=='open'}">
        <s:stroke>
            <s:SolidColorStroke color="0xCCCCCC"/>
        </s:stroke>
    </s:Line>
    <s:RichText id="pathTxt"
                            width="100%"
                            textFlow="{TextConverter.importToFlow('&lt;b&gt;'+R.get('PATH')+': &lt;/b&gt;'+evidence.data.path, TextConverter.TEXT_FIELD_HTML_FORMAT)}"
                            fontSize="12"/>
    <s:Line width="100%">
        <s:stroke>
            <s:SolidColorStroke color="0xCCCCCC"/>
        </s:stroke>
    </s:Line>
    <s:RichText id="sizeTxt"
                            width="100%"
                            textFlow="{TextConverter.importToFlow('&lt;b&gt;'+R.get('SIZE')+': &lt;/b&gt;'+size, TextConverter.TEXT_FIELD_HTML_FORMAT)}"
                            fontSize="12"/>
    <s:Line width="100%">
        <s:stroke>
            <s:SolidColorStroke color="0xCCCCCC"/>
        </s:stroke>
    </s:Line>
    <s:HGroup visible="{evidence.data.type=='capture' &amp;&amp; Console.currentSession.user.is_view_export()}"
                        includeInLayout="{evidence.data.type=='capture' &amp;&amp; Console.currentSession.user.is_view_export()}"
                        verticalAlign="middle">
        <s:Button id="downloadBtn"
                            label="Download"
                            click="getFile()"/>
        <s:Label id="downloadStatusTxt"/>
    </s:HGroup>
    <s:RichText id="accessTxt"
                            width="100%"
                            textFlow="{TextConverter.importToFlow('&lt;b&gt;Access Mask: &lt;/b&gt;'+accessMask, TextConverter.TEXT_FIELD_HTML_FORMAT)}"
                            fontSize="12"
                            visible="{evidence.data.type=='open'}"
                            includeInLayout="{evidence.data.type=='open'}"/>

    <s:Line width="100%"
                    visible="{evidence.data.type=='open'}"
                    includeInLayout="{evidence.data.type=='open'}">
        <s:stroke>
            <s:SolidColorStroke color="0xCCCCCC"/>
        </s:stroke>
    </s:Line>
    <s:RichText id="contentTxt"
                            width="100%"
                            textFlow="{TextConverter.importToFlow('&lt;b&gt;'+R.get('CONTENT')+': &lt;/b&gt;'+content, TextConverter.TEXT_FIELD_HTML_FORMAT)}"
                            fontSize="12"
                            visible="{evidence.data.type=='open'}"
                            includeInLayout="{evidence.data.type=='open'}"/>
    <s:RichText id="translationTxt"
                            width="100%"
                            textFlow="{TextConverter.importToFlow('&lt;i&gt;&lt;b&gt;'+R.get('CONTENT')+': &lt;/b&gt;'+translation+'&lt;/i&gt;', TextConverter.TEXT_FIELD_HTML_FORMAT)}"
                            fontSize="12"
                            visible="{evidence.data.type=='open'}"
                            includeInLayout="{evidence.data.type=='open'}"/>
</s:BorderContainer>