断点アップロード中のjavascriptコード


//    ,  :"/upload"
var contextPath ;
//             id
var ratePercent_id;
//        
var isUploadComplete = 'false';
//              (   ,    )
var getRateInterval = 3000;
//                     
var checkIsInstall_time_delay = 2000;
//        ,          url
var _callbackURL;
//        url
var getAuthorityURL;
//                     
var getFilePathInterval = 1000;
//               key
var key = Date.parse(new Date())+'-'+Math.random();
//         URL:
var _URL4UploadServerInfo;
//getAuthority   
var getAuthorityResponse;
var isShowRateLocal;
/**
 *               、     、      id
 * @param {Object} path     
 * @param {Object} linkId    
 * @param {Object} rateId  
 * @param {Object} fileNameId   
 * @param {Object} percentId   
 */
function initContextPathAndIds(URL4UploadServerInfo,callbackURL,ratePercentId){
	if(! URL4UploadServerInfo){
		alert('   URL4UploadServerInfo  ');
	}
	if(! callbackURL){
		alert('   callbackURL  ');
	}
	_URL4UploadServerInfo = URL4UploadServerInfo;
	_callbackURL = callbackURL;
	if(ratePercentId){
		ratePercent_id = ratePercentId;
	}
	loadComplete();
}

/**
 *   form      ,           
 */
function findAllFormElements(){
	var params = '';
	var element;
	//  input type=text  (  name  )
	$("input:text[name]").each(function(){
		element = $(this);
		//    value   
		if(! element.val()){
			return;
		}
		//       
		if(params){
			params += '&'+element.attr('name')+'='+element.val();
		//      
		}else{
			params += element.attr('name')+'='+element.val();
		}
	});
	//  input type=password  (  name  )
	$("input:password[name]").each(function(){
		element = $(this);
		//    value   
		if(! element.val()){
			return;
		}
		//       
		if(params){
			params += '&'+element.attr('name')+'='+element.val();
		//      
		}else{
			params += element.attr('name')+'='+element.val();
		}
	});
	//  input type=hidden  (  name  )
	$("input[type='hidden'][name]").each(function(){
		element = $(this);
		//    value   
		if(! element.val()){
			return;
		}
		//       
		if(params){
			params += '&'+element.attr('name')+'='+element.val();
		//      
		}else{
			params += element.attr('name')+'='+element.val();
		}
	});
	//  input type=radio  (  name  )
	$("input:radio[name]:checked").each(function(){
		element = $(this);
		//    value   
		if(! element.val()){
			return;
		}
		//       
		if(params){
			params += '&'+element.attr('name')+'='+element.val();
		//      
		}else{
			params += element.attr('name')+'='+element.val();
		}
	});
	//  input type=checkbox  (  name  )
	$("input:checkbox[name]:checked").each(function(){
		element = $(this);
		//    value   
		if(! element.val()){
			return;
		}
		//       
		if(params){
			params += '&'+element.attr('name')+'='+element.val();
		//      
		}else{
			params += element.attr('name')+'='+element.val();
		}
	});
	//  select option  (  name  )
	$("select[name] option:selected").each(function(){
		element = $(this);
		//    value   
		if(! element.val()){
			return;
		}
		if(! element.parent("[name]")){
			return;
		}
		//       
		if(params){
			params += '&'+element.parent("[name]").attr('name')+'='+element.val();
		//      
		}else{
			params += element.attr('name')+'='+element.val();
		}
	});
	return params;
}
var getFilePathTimeout;
/**
 *    
 */
function loadComplete(){
	var file;
	if(! key){
		key = Date.parse(new Date())+'-'+Math.random();
	}
	$("input:file").each(function(i){
		file = $(this);
		file.bind('click',function(){
			try{
				//    ,           
				if(! file.attr("id")){
					alert('    file    id');
					return false;
				}
				//   1000ms,                ,  IsInstall     
				setTimeout("checkIsInstall()",checkIsInstall_time_delay);
				//            
				window.location = 'Luban://local_do=fileupload&choosefile=true&key='+key+"&contextPath="+ contextPath+"&id="+file.attr("id");
				if(getFilePathTimeout){
					clearTimeout(getFilePathTimeout);
				}
				//   file  , 500ms         
				getFilePathTimeout = setTimeout("getFilePathFromServer()",500);
			}catch(e){}
			return false;
		});
	});
	// _URL4UploadServerInfo    get  ,       
	$.get(_URL4UploadServerInfo,function(data){
		if(data){
			//    :uploadServerDomain=http://u.myluban.com&&URL4ApplyUploadAuth=http://www.google.com/applyUploadAuth4LubanAdmin
			contextPath = data.split('&&')[0].split('=')[1];
			getAuthorityURL = data.split('&&')[1].split('=')[1];
		}
	},"text");
}

/**
 *       
 */
function getAuthority(getAuthorityURL,href){
	$.get(getAuthorityURL,function(data){
		if(data){
			//    :authSource=      &&auth2User=   &&auth2UserType=2&&fileUpload2URL=http://u.myluban.com/fileupload&&
			//						authGenerateTime=1370422339758&&authExpiredTimeLen=30000&&sign=md5
			//               &  ,   &&   ##
			getAuthorityResponse = data.replace(/&&/g,'##');
			// href      
			sendHrefToServer(href);
		}
	},"text");
}
/**
 *                 ,      
 */
function getFilePathFromServer(){
	var url = contextPath+'/lubanFileUpload/getFilePath';
	if(! key){
		key = Date.parse(new Date())+'-'+Math.random();
	}
	var param = {'key':key};
	//          ,                        
	$.post(url,param,function(data){
		if(data){
			// data  json  
			data = eval("("+data+")");
			//     ,      
			if("cancel" == data.status){
				if(getFilePathTimeout){
					clearTimeout(getFilePathTimeout);
				}
				return;
			//     ,     ,     
			}else if("confirm" == data.status){
//				createAndShowFloatDiv(data.id,data.filePath);
				//alert(data.filePath);
				if(getFilePathTimeout){
					clearTimeout(getFilePathTimeout);
				}
				return;
			}
		}
		//          ,    
		if(getFilePathTimeout){
			clearTimeout(getFilePathTimeout);
		}
		getFilePathTimeout = setTimeout("getFilePathFromServer()",getFilePathInterval);
	},"text");
}
/**
 *         
 */
function createAndShowFloatDiv(id,filePath){
	var width = $("#"+id).width()*0.75;
	var height= $("#"+id).height();
	var top = $("#"+id).offset().top;
	var left =  $("#"+id).offset().left;
}
/**
 *            
 */
function upload(){
	//   :uploadhelper://fileName=aaa.txt&fileName=bbb.txt&param=p1&param=p2
	var href = '';
	//     file,             
	if(isShowRateLocal){
		href += "isShowRateLocal=true";
	}
	if(_callbackURL){
		if(href){
			href += ("&callbackURL="+_callbackURL);
		}else{
			href += ("callbackURL="+_callbackURL);
		}
	}
	var param = findAllFormElements();
	if(param){
		if(href){
			href += ("&"+param);
		}else{
			href += (param);
		}
	}
	getAuthority(getAuthorityURL,href);
}

//                  
var getUploadRateTimeout ;

/**
 *              ,            
 */
function confirmUpload(){
	upload();
	return false;
}
/**
 *             ,            
 */
function checkIsInstall(isUploadOrNot){
	var url = contextPath+'/lubanFileUpload/getIsInstall?key='+key;
	$.get(url,function(data){
		if(data != 'true'){
			var result = confirm('             ,          ,               ,           ,' +
								'     ,            ,  :                  ');
			if(result){
				window.location = contextPath+"/lubanFileUpload/downloadClient";
			}
		}
		//          ,                  ,         
		//          ,         
		if(isUploadOrNot){
			if(getUploadRateTimeout){
				clearTimeout(getUploadRateTimeout);
			}
			getUploadRateTimeout = setTimeout("getUploadRate()",0);
		}
	},"text");
}
/**
 *       
 */
 function showRate_manual(){
	if(getUploadRateTimeout){
		clearTimeout(getUploadRateTimeout);
	}
	getUploadRateTimeout = setTimeout("getUploadRate()",0);
 }
/**
 *            
 */
function sendHrefToServer(href){
	if(! getAuthorityResponse){
		alert("        ");
		//       Url  ,       
		return;
	}
	if(! contextPath){
		alert("           ");
		//    contextPath  
		return;
	}
	//        url  
	href += ("&getAuthorityResponse="+getAuthorityResponse);
	href = "Luban://"+encodeURI(encodeURI(href));
	var url = contextPath+'/lubanFileUpload/setHrefValue';
	if(! key){
		key = Date.parse(new Date())+'-'+Math.random();
	}
	var param = {'href':href,'key':key};
	//          ,                        
	$.post(url,param,function(){
		//   1000ms,                ,  IsInstall     
		setTimeout("checkIsInstall('upload')",checkIsInstall_time_delay);
		window.location = "Luban://getArgumentsFromServer=true&local_do=fileupload&contextPath="+ contextPath +"&key="+key;
	},"text");
}
/**
 *   ajax          
 */ 
function getUploadRate(){
	//           id    ,               
	if(! ratePercent_id){
		//  ratePercent_id   ,                  
		if(getUploadRateTimeout){
			//              ,       
			clearTimeout(getUploadRateTimeout);
		}
		return;
	}
	if(key){
		var url = contextPath+'/lubanFileUpload/getUploadRateAndPercent?time='+Date.parse(new Date())+"&key="+key;
	$.get(url,function(data){
		showRate(data);
	},"text");
	}
}
/**
 *         
 */
function showRate(data){
	if(data){
		var result = eval("("+data+")");
		$("#"+ratePercent_id).html('   :'+result.fileName+'<br/>'+'    :'+result.rate+' kb/s'+'<br/>'+'     :'+result.percent+' %');
		if(result.isCompleted == 'true'){
			if(getUploadRateTimeout){
				clearTimeout(getUploadRateTimeout);
			}
			isUploadComplete = 'true';
			//         
			fileUploadComplete();
			//  
			window.location = window.location;
		}else if(result.isCompleted == 'interrupt'){
			if(getUploadRateTimeout){
				clearTimeout(getUploadRateTimeout);
			}
			isUploadComplete = 'break';
			//         
			fileUploadInterrupted();
			//  
			window.location = window.location;
		}else{
			if(getUploadRateTimeout){
				clearTimeout(getUploadRateTimeout);
			}
			getUploadRateTimeout = setTimeout("getUploadRate()",getRateInterval);
		}
	}else{
		if(getUploadRateTimeout){
				clearTimeout(getUploadRateTimeout);
			}
		getUploadRateTimeout = setTimeout("getUploadRate()",getRateInterval);
	}
}
function fileUploadComplete(){}
function fileUploadInterrupted(){}
/**
 *      JSESSIONID
 * @return {TypeName} 
 */
function getSessionId(){
	var c_name = 'JSESSIONID';
	if(document.cookie.length>0){
	   c_start=document.cookie.indexOf(c_name + "=");
	   if(c_start!=-1){ 
	     c_start=c_start + c_name.length+1 ;
	     c_end=document.cookie.indexOf(";",c_start);
	     if(c_end==-1) c_end=document.cookie.length;
	     return unescape(document.cookie.substring(c_start,c_end));
	   }
	}
}