pull/1238/head
gedoor 3 years ago
parent 52aafdafc1
commit 9906247ba7
  1. 26
      app/src/main/assets/web/uploadBook/js/common.js
  2. 2
      app/src/main/assets/web/uploadBook/js/html5_fun.js
  3. 7
      app/src/main/assets/web/uploadBook/js/langSwich.js
  4. 25
      app/src/main/assets/web/uploadBook/js/swf_fun.js
  5. 9
      app/src/main/java/io/legado/app/api/controller/BookController.kt
  6. 11
      app/src/main/java/io/legado/app/web/HttpServer.kt

@ -6,7 +6,7 @@
var config = { var config = {
fileTypes: "txt|epub|umd", //允许上传的文件格式 "txt|epub" // |doc|docx|wps|xls|xlsx|et|ppt|pptx|dps fileTypes: "txt|epub|umd", //允许上传的文件格式 "txt|epub" // |doc|docx|wps|xls|xlsx|et|ppt|pptx|dps
//url : "http://"+location.host+"?action=addBook",//"http://localhost/t/post.php",// //url : "http://"+location.host+"?action=addBook",//"http://localhost/t/post.php",//
url: "http://192.168.1.16:1122/addLocalBook", url: "../addLocalBook",
fileLimitSize: 500 * 1024 * 1024 fileLimitSize: 500 * 1024 * 1024
} }
@ -41,8 +41,7 @@ $.ajax({
}) })
//统计文件大小 //统计文件大小
function countFileSize(fileSize) function countFileSize(fileSize) {
{
var KB = 1024; var KB = 1024;
var MB = 1024 * 1024; var MB = 1024 * 1024;
if (KB >= fileSize) { if (KB >= fileSize) {
@ -55,8 +54,7 @@ function countFileSize(fileSize)
} }
//如果文件太长进行截取 //如果文件太长进行截取
function substr_string(name) function substr_string(name) {
{
var maxLen = 15; var maxLen = 15;
var len = name.length; var len = name.length;
if (len < 17) return name; if (len < 17) return name;
@ -101,13 +99,11 @@ function checkFile(file) {
* @param object file 文件对象 * @param object file 文件对象
* @param int type 0 是swf 上传的1 是html5上传的 * @param int type 0 是swf 上传的1 是html5上传的
*/ */
function fileQueued(file, type) function fileQueued(file, type) {
{
var size = 0, fid = file.id, name = ""; var size = 0, fid = file.id, name = "";
type = type || 0; type = type || 0;
if(file != undefined ) if (file != undefined) {
{
//计算文件大小 单位MB //计算文件大小 单位MB
size = countFileSize(file.size); size = countFileSize(file.size);
name = substr_string(file.name) name = substr_string(file.name)
@ -137,8 +133,7 @@ function fileQueued(file, type)
} }
} }
function changeTrBackGroundColor() function changeTrBackGroundColor() {
{
var getTr = document.getElementById("tableStyle").getElementsByTagName("dl"); var getTr = document.getElementById("tableStyle").getElementsByTagName("dl");
trNum = getTr.length; trNum = getTr.length;
for (var i = 0; i < trNum; i++) { for (var i = 0; i < trNum; i++) {
@ -151,8 +146,7 @@ function changeTrBackGroundColor()
//上传时返回的状态 //上传时返回的状态
function uploadProgress(file, bytesLoaded, bytesTotal) function uploadProgress(file, bytesLoaded, bytesTotal) {
{
if (!$("#progress_bar_p_" + file.id).hasClass("orange")) { if (!$("#progress_bar_p_" + file.id).hasClass("orange")) {
$("#progress_bar_p_" + file.id).addClass("orange"); $("#progress_bar_p_" + file.id).addClass("orange");
} }
@ -163,16 +157,14 @@ function uploadProgress(file, bytesLoaded, bytesTotal)
//上传成功 //上传成功
function uploadSuccess(file, serverData, res) function uploadSuccess(file, serverData, res) {
{
var id = "handle_button_" + file.id; var id = "handle_button_" + file.id;
$("#" + id).replaceWith("<dd>" + jsonLang.t10 + "</dd>") $("#" + id).replaceWith("<dd>" + jsonLang.t10 + "</dd>")
} }
//取消上传 //取消上传
function userCancelUpload(file_id, type) function userCancelUpload(file_id, type) {
{
if (type == 0) { if (type == 0) {
SWFFuns.cancelUpload(file_id); SWFFuns.cancelUpload(file_id);

@ -140,7 +140,7 @@
XHR.upload.addEventListener("loadend", loadend, false); XHR.upload.addEventListener("loadend", loadend, false);
XHR.upload.addEventListener("loadstart", loadstart, false); XHR.upload.addEventListener("loadstart", loadstart, false);
XHR.open("POST", config.url); XHR.open("POST", config.url);
XHR.setRequestHeader("Content-Type","application/octet-stream"); XHR.setRequestHeader("Content-Type", "multipart/mixed stream");
XHR.send(fd); XHR.send(fd);
XHR.onreadystatechange = function () { XHR.onreadystatechange = function () {

@ -10,13 +10,15 @@
/** /**
* 语言包 * 语言包
*/ */
jsonEn = { t0:"WiFi Upload",t1:"Documents",t2:"Name",t3:"Size",t4:"Operation", jsonEn = {
t0: "WiFi Upload", t1: "Documents", t2: "Name", t3: "Size", t4: "Operation",
t5: "Supported Format:TXT、EPUB、UMD", t6: "Drag here and Drop to upload", t5: "Supported Format:TXT、EPUB、UMD", t6: "Drag here and Drop to upload",
t7: "You choosed [txt] files ,we only can upload [txt] of them。\nPlease choose TXT、EPUB、UMD files, file name cannot be repeated.", t7: "You choosed [txt] files ,we only can upload [txt] of them。\nPlease choose TXT、EPUB、UMD files, file name cannot be repeated.",
t8: "Failed", t9: "Cancel", t10: "Done", t11: "One of books is over 500MB", t12: "Bad file mode", t8: "Failed", t9: "Cancel", t10: "Done", t11: "One of books is over 500MB", t12: "Bad file mode",
t13: "File already exists", t14: "Canceled" t13: "File already exists", t14: "Canceled"
}; };
jsonZh = { t0:"WiFi 下载",t1:"文档",t2:"名字",t3:"大小",t4:"操作", jsonZh = {
t0: "传书", t1: "文档", t2: "名字", t3: "大小", t4: "操作",
t5: "支持类型:TXT、EPUB、UMD", t6: "拖至此处上传", t5: "支持类型:TXT、EPUB、UMD", t6: "拖至此处上传",
t7: "你选择了[txt]个文件,我们只能上传其中的[txt]。\n请选择 TXT、EPUB、UMD 类型的文件,文件的名字不能重复。", t7: "你选择了[txt]个文件,我们只能上传其中的[txt]。\n请选择 TXT、EPUB、UMD 类型的文件,文件的名字不能重复。",
t8: "失败", t9: "取消", t10: "完成", t11: "上传文件不能大于500MB", t12: "无效的文件格式", t8: "失败", t9: "取消", t10: "完成", t11: "上传文件不能大于500MB", t12: "无效的文件格式",
@ -85,4 +87,3 @@
//执行 //执行
langSwich(language); langSwich(language);

@ -6,7 +6,6 @@ var swfSelectCount = 0;// 当前选中的文件数量
window.onload = function () { window.onload = function () {
var settings = { var settings = {
flash_url : "js/swfupload.swf",
upload_url: config.url, upload_url: config.url,
/*post_params: {"PHPSESSID" : "<?php echo session_id(); ?>"},*/ /*post_params: {"PHPSESSID" : "<?php echo session_id(); ?>"},*/
file_size_limit: config.fileLimitSize + " B", file_size_limit: config.fileLimitSize + " B",
@ -46,20 +45,17 @@ window.onload = function() {
//上传完成 //上传完成
function uploadComplete(file,server,response) function uploadComplete(file, server, response) {
{
//继续下一个文件的上传 //继续下一个文件的上传
this.startUpload(); this.startUpload();
} }
//完成队列里的上传 //完成队列里的上传
function queueComplete(numFilesUploaded) function queueComplete(numFilesUploaded) {
{
} }
function userStartUpload(file_id) function userStartUpload(file_id) {
{
swfu.startUpload(file_id); swfu.startUpload(file_id);
} }
@ -67,8 +63,7 @@ function userStartUpload(file_id)
function fileQueueError(file, errorCode, message) function fileQueueError(file, errorCode, message) {
{
switch (errorCode) { switch (errorCode) {
case -100: case -100:
//alert("Over 100 books"); //alert("Over 100 books");
@ -81,22 +76,18 @@ function fileQueueError(file, errorCode, message)
} }
} }
//入列完毕 //入列完毕
function fileDialogComplete(numFilesSelected, numFilesQueued) function fileDialogComplete(numFilesSelected, numFilesQueued) {
{ if (numFilesSelected > 0) {
if (numFilesSelected > 0)
{
this.startUpload() this.startUpload()
} }
} }
//开始上传 //开始上传
function uploadStart(file) function uploadStart(file) {
{
return true; return true;
} }
//上传出错 //上传出错
function uploadError(file, errorCode, message) function uploadError(file, errorCode, message) {
{
switch (errorCode) { switch (errorCode) {
case SWFUpload.UPLOAD_ERROR.HTTP_ERROR: case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
errorMessage = "Error"; errorMessage = "Error";

@ -197,8 +197,9 @@ object BookController {
DiskFileItemFactory(0, LocalBook.cacheFolder) DiskFileItemFactory(0, LocalBook.cacheFolder)
} }
fun addLocalBook(session: NanoHTTPD.IHTTPSession): ReturnData { fun addLocalBook(session: NanoHTTPD.IHTTPSession, postData: String?): ReturnData {
val returnData = ReturnData() val returnData = ReturnData()
try {
NanoFileUpload(bookFileFactory).parseRequest(session).forEach { NanoFileUpload(bookFileFactory).parseRequest(session).forEach {
val path = FileUtils.getPath(LocalBook.cacheFolder, it.name) val path = FileUtils.getPath(LocalBook.cacheFolder, it.name)
val nameAuthor = LocalBook.analyzeNameAuthor(it.name) val nameAuthor = LocalBook.analyzeNameAuthor(it.name)
@ -217,6 +218,12 @@ object BookController {
if (book.isUmd()) UmdFile.upBookInfo(book) if (book.isUmd()) UmdFile.upBookInfo(book)
appDb.bookDao.insert(book) appDb.bookDao.insert(book)
} }
} catch (e: Exception) {
e.printStackTrace()
return returnData.setErrorMsg(
e.localizedMessage ?: appCtx.getString(R.string.unknown_error)
)
}
return returnData.setData(true) return returnData.setData(true)
} }

@ -21,8 +21,8 @@ class HttpServer(port: Int) : NanoHTTPD(port) {
var uri = session.uri var uri = session.uri
try { try {
when (session.method.name) { when (session.method) {
"OPTIONS" -> { Method.OPTIONS -> {
val response = newFixedLengthResponse("") val response = newFixedLengthResponse("")
response.addHeader("Access-Control-Allow-Methods", "POST") response.addHeader("Access-Control-Allow-Methods", "POST")
response.addHeader("Access-Control-Allow-Headers", "content-type") response.addHeader("Access-Control-Allow-Headers", "content-type")
@ -30,7 +30,7 @@ class HttpServer(port: Int) : NanoHTTPD(port) {
//response.addHeader("Access-Control-Max-Age", "3600"); //response.addHeader("Access-Control-Max-Age", "3600");
return response return response
} }
"POST" -> { Method.POST -> {
val files = HashMap<String, String>() val files = HashMap<String, String>()
session.parseBody(files) session.parseBody(files)
val postData = files["postData"] val postData = files["postData"]
@ -40,11 +40,11 @@ class HttpServer(port: Int) : NanoHTTPD(port) {
"/saveSources" -> SourceController.saveSources(postData) "/saveSources" -> SourceController.saveSources(postData)
"/saveBook" -> BookController.saveBook(postData) "/saveBook" -> BookController.saveBook(postData)
"/deleteSources" -> SourceController.deleteSources(postData) "/deleteSources" -> SourceController.deleteSources(postData)
"/addLocalBook" -> BookController.addLocalBook(session) "/addLocalBook" -> BookController.addLocalBook(session, postData)
else -> null else -> null
} }
} }
"GET" -> { Method.GET -> {
val parameters = session.parameters val parameters = session.parameters
returnData = when (uri) { returnData = when (uri) {
@ -58,6 +58,7 @@ class HttpServer(port: Int) : NanoHTTPD(port) {
else -> null else -> null
} }
} }
else -> Unit
} }
if (returnData == null) { if (returnData == null) {

Loading…
Cancel
Save