/** * Yona, 21st Century Project Hosting SW *
* Copyright Yona & Yobi Authors & NAVER Corp. & NAVER LABS Corp. * https://yona.io **/ yobi.Files = (function(){ var htVar = {}; var htElements = {}; var htHandlers = {}; /** * initialize fileUploader * * @param {Hash Table} htOptions * @param {String} htOptions.sListURL * @param {String} htOptions.sUploadURL */ function _init(htOptions){ htOptions = htOptions || {}; htVar.sListURL = htOptions.sListURL; htVar.sUploadURL = htOptions.sUploadURL; htVar.htUploadOpts = htOptions.htUploadOpts || {"dataType": "json"}; // XMLHttpRequest2 file upload // The FileReader API is not actually used, but works as feature detection. // Check for window.ProgressEvent instead to detect XHR2 file upload capability // ref: http://blueimp.github.io/jQuery-File-Upload htVar.bXHR2 = !!(window.ProgressEvent && window.FileReader) && !!window.FormData; // HTTPS connection is required for XHR upload on MSIE Browsers // even if FormData feature available. if(navigator.userAgent.toLowerCase().indexOf("trident") > -1){ htVar.bXHR2 = htVar.bXHR2 && (location.protocol.toLowerCase().indexOf("https") > -1); } // HTML5 FileAPI required htVar.bDroppable = (typeof window.File != "undefined") && htVar.bXHR2; // onpaste & XHR2 required htVar.bPastable = (typeof document.onpaste != "undefined") && htVar.bXHR2 && (navigator.userAgent.indexOf("FireFox") === -1); // and not FireFox // maximum filesize (<= 2,147,483,454 bytes = 2Gb) htVar.nMaxFileSize = htOptions.maxFileSize || 2147483454; } /** * Returns Environment information * @return {Hash Table} */ function _getEnv(){ return htVar; } /** * Upload files * * @param {Variant} oFiles FileList or File Object, HTMLInputElement(IE) * @param {String} sNamespace (Optional) */ function _uploadFile(oFiles, sNamespace){ if(oFiles && oFiles.length){ for(var i = 0; i < oFiles.length; i++){ _uploadSingleFile(oFiles[i], _getSubmitId(), sNamespace); } } else { _uploadSingleFile(oFiles, _getSubmitId(), sNamespace); } } /** * Upload single file with specified submitId * * @param {File} oFile * @param {Number} nSubmitId * @param {String} sNamespace (Optional) */ function _uploadSingleFile(oFile, nSubmitId, sNamespace){ // append file on list if(oFile){ oFile.nSubmitId = nSubmitId || _getSubmitId(); } // fireEvent: beforeUpload var bEventResult = _fireEvent("beforeUpload", { "oFile": oFile, "nSubmitId": oFile ? oFile.nSubmitId : nSubmitId }, sNamespace); if(bEventResult === false){ // upload cancel by event handler return false; } return htVar.bXHR2 ? _uploadFileXHR(nSubmitId, oFile, sNamespace) : _uploadFileForm(nSubmitId, oFile, sNamespace); } /** * Upload file with XHR2 * available in IE 10+, FF4+, Chrome7+, Safari5+ * Reference: http://caniuse.com/xhr2 * * @param {Number} nSubmitId * @param {File} oFile * @param {String} sNamespace */ function _uploadFileXHR(nSubmitId, oFile, sNamespace){ // check maximum filesize (<= 2,147,483,454 bytes) if available if(oFile.size && oFile.size > htVar.nMaxFileSize){ return _onErrorSubmit(nSubmitId, { "status" : humanize.filesize(oFile.size), "statusText": Messages("error.toolargefile", humanize.filesize(htVar.nMaxFileSize)) }, sNamespace); } var oData = new FormData(); var filename = oFile.name === 'image.png' ? nSubmitId + ".png" : oFile.name; oData.append("filePath", oFile, filename); $.ajax({ "type" : "post", "url" : htVar.sUploadURL, "data" : oData, "cache": false, "processData": false, "contentType": false, "success": function(oRes){ _onSuccessSubmit(nSubmitId, oRes, sNamespace); }, "error": function(oRes){ _onErrorSubmit(nSubmitId, oRes, sNamespace); }, "xhr": function(){ var oXHR = $.ajaxSettings.xhr(); if(oXHR.upload){ oXHR.upload.addEventListener("progress", function(weEvt){ if(weEvt.lengthComputable){ _onUploadProgress(nSubmitId, Math.ceil((weEvt.loaded / weEvt.total) * 100), sNamespace); } }, false); } return oXHR; } }); } /** * Upload file with $.ajaxForm * available in almost browsers, except Safari on OSX. * Reference: http://malsup.com/jquery/form/ * * @param {Number} nSubmitId * @param {HTMLElement} elFile * @param {String} sNamespace */ function _uploadFileForm(nSubmitId, elFile, sNamespace){ var htElement = htElements[sNamespace]; if(!htElement.welInputFile && !elFile){ return false; } var welInputFile = htElement.welInputFile || $(elFile); var welInputFileClone = welInputFile.clone(); var welForm = $('