From 4ec6246b3839c9191059a36f4974217b15aa730f Mon Sep 17 00:00:00 2001 From: Jihan Kim Date: Wed, 5 Feb 2014 17:24:03 +0900 Subject: [PATCH] Fix checker whether selected file is image before upload --- public/javascripts/common/yobi.Common.js | 42 ++++++++++++++++++- .../service/yobi.project.Setting.js | 4 +- .../javascripts/service/yobi.user.Setting.js | 2 +- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/public/javascripts/common/yobi.Common.js b/public/javascripts/common/yobi.Common.js index 737d6fb3f..acf737f2b 100644 --- a/public/javascripts/common/yobi.Common.js +++ b/public/javascripts/common/yobi.Common.js @@ -315,6 +315,45 @@ $yobi = yobi.Common = (function(){ return htVar.welHSC.text(sHTML).html(); } + /** + * Get whether the file is image with MIME type, and filename extension. + * returns boolean or null if unavailable to determine result + * + * @param {Variant} vFile File object or HTMLElement + * @returns {Boolean|Null} + */ + function isImageFile(vFile){ + // if vFile is File Object + if(typeof window.File !== "undefined" && vFile instanceof window.File){ + return (vFile.type.indexOf("image/") !== 0); + } + + // if vFile is HTMLElement + var welFile = $(vFile); + var oFileList = welFile.prop("files"); + + if(oFileList && oFileList.length){ + var bResult = true; + + for(var i = 0, nLength = oFileList.length; i < nLength; i++){ + bResult = bResult && isImageFile(oFileList[i]); + } + + return bResult; + } + + // if cannot find MIME type from File object + // get whether filename ends with extension looks like image file + // like as .gif, .bmp, .jpg, .jpeg, .png. + if(typeof welFile.val() === "string"){ + htVar.rxImgExts = htVar.rxImgExts || /\.(gif|bmp|jpg|jpeg|png)$/i; + return htVar.rxImgExts.test(welFile.val()); + } + + // Unavailable to detect mimeType + return null; + } + /* public Interface */ return { "setScriptPath" : setScriptPath, @@ -330,7 +369,8 @@ $yobi = yobi.Common = (function(){ "notify" : notify, "nl2br" : nl2br, "tmpl" : processTpl, - "htmlspecialchars": htmlspecialchars + "htmlspecialchars": htmlspecialchars, + "isImageFile": isImageFile }; })(); diff --git a/public/javascripts/service/yobi.project.Setting.js b/public/javascripts/service/yobi.project.Setting.js index db414df3e..c710364d3 100644 --- a/public/javascripts/service/yobi.project.Setting.js +++ b/public/javascripts/service/yobi.project.Setting.js @@ -32,7 +32,6 @@ * 정규식 변수는 한번만 선언하는게 성능 향상에 도움이 됩니다 */ function _initVar(htOptions){ - htVar.rxLogoExt = /\.(gif|bmp|jpg|jpeg|png)$/i; htVar.rxPrjName = /^[0-9A-Za-z-_\.]+$/; htVar.aReservedWords = [".", "..", ".git"]; } @@ -80,8 +79,7 @@ function _onChangeLogoPath(){ var welTarget = $(this); - // 확장자 규칙 검사 - if(!htVar.rxLogoExt.test(welTarget.val())){ + if($yobi.isImageFile(welTarget) === false){ $yobi.showAlert(Messages("project.logo.alert")); welTarget.val(''); return; diff --git a/public/javascripts/service/yobi.user.Setting.js b/public/javascripts/service/yobi.user.Setting.js index c7ecf5a39..0a71f52e2 100644 --- a/public/javascripts/service/yobi.user.Setting.js +++ b/public/javascripts/service/yobi.user.Setting.js @@ -87,7 +87,7 @@ * @return {Boolean} */ function _onAvatarBeforeUpload(htData){ - if(htData.oFile && htData.oFile.type.indexOf("image/") !== 0){ + if($yobi.isImageFile(htData.oFile) === false){ _onAvatarUploadError(Messages("user.avatar.onlyImage")); return false; }