提交 869e22ea 编写于 作者: J jackjintai

android:新增相对路径

上级 719a7993
......@@ -72,14 +72,14 @@ val DoKitFileRouter: Application.() -> Unit = {
* index
*/
get("/") {
call.respond(IndexAction.createIndexInfo())
call.respond(IndexAction.indexInfoRes())
}
/**
* 获取设备详情
*/
get("/getDeviceInfo") {
call.respond(DeviceInfoAction.createDeviceInfo())
call.respond(DeviceInfoAction.deviceInfoRes())
}
/**
......@@ -87,11 +87,11 @@ val DoKitFileRouter: Application.() -> Unit = {
*/
get("/getFileList") {
val queryParameters = call.request.queryParameters
val filePath = queryParameters["filePath"]
if (filePath.isNullOrBlank()) {
call.respond(RequestErrorAction.createErrorInfo("filePath is not standard"))
val dirPath = FileManagerUtil.absoluteRootPath(queryParameters["dirPath"])
if (dirPath.isBlank()) {
call.respond(RequestErrorAction.createErrorInfo("dirPath is not standard"))
} else {
call.respond(FileListAction.createFileList(filePath))
call.respond(FileListAction.fileListRes(dirPath))
}
}
......@@ -100,10 +100,10 @@ val DoKitFileRouter: Application.() -> Unit = {
*/
get("/getFileDetail") {
val queryParameters = call.request.queryParameters
val dirPath = queryParameters["filePath"]
val dirPath = FileManagerUtil.absoluteRootPath(queryParameters["dirPath"])
val fileType = queryParameters["fileType"]
val fileName = queryParameters["fileName"]
call.respond(FileDetailAction.createFileDetailInfo("$dirPath${File.separator}$fileName", fileType))
call.respond(FileDetailAction.fileDetailInfoRes("$dirPath${File.separator}$fileName", fileType))
}
/**
......@@ -111,7 +111,7 @@ val DoKitFileRouter: Application.() -> Unit = {
*/
post("/createFolder") {
val params = call.receive<DirInfo>()
val dirPath = params.filePath
val dirPath = FileManagerUtil.absoluteRootPath(params.dirPath)
val fileName = params.fileName
call.respond(CreateFolderAction.createFolderRes(dirPath, fileName))
}
......@@ -122,7 +122,6 @@ val DoKitFileRouter: Application.() -> Unit = {
post("/uploadFile") {
val multipart = call.receiveMultipart()
call.respond(UploadFileAction.uploadFileRes(multipart))
}
/**
......@@ -130,7 +129,7 @@ val DoKitFileRouter: Application.() -> Unit = {
*/
get("/downloadFile") {
val queryParameters = call.request.queryParameters
val dirPath = queryParameters["filePath"]
val dirPath = FileManagerUtil.absoluteRootPath(queryParameters["dirPath"])
val fileName = queryParameters["fileName"]
val file = File("$dirPath${File.separator}$fileName")
......@@ -151,10 +150,10 @@ val DoKitFileRouter: Application.() -> Unit = {
*/
post("/deleteFile") {
val params = call.receive<DirInfo>()
val dirPath = params.filePath
val dirPath = FileManagerUtil.absoluteRootPath(params.dirPath)
val fileName = params.fileName
val filePath = "$dirPath${File.separator}$fileName"
call.respond(DeleteFileAction.createDeleteRes(filePath, dirPath, fileName))
call.respond(DeleteFileAction.deleteFileRes(filePath, dirPath, fileName))
}
/**
......@@ -162,10 +161,10 @@ val DoKitFileRouter: Application.() -> Unit = {
*/
post("/rename") {
val fileInfo = call.receive<RenameFileInfo>()
val dirPath = fileInfo.filePath
val dirPath = FileManagerUtil.absoluteRootPath(fileInfo.dirPath)
val oldName = fileInfo.oldName
val filePath = "$dirPath${File.separator}$oldName"
call.respond(RenameFileAction.renameRes(fileInfo.newName, filePath))
call.respond(RenameFileAction.renameFileRes(fileInfo.newName, filePath))
}
......@@ -174,11 +173,11 @@ val DoKitFileRouter: Application.() -> Unit = {
*/
post("/saveFile") {
val saveFileInfo = call.receive<SaveFileInfo>()
val dirPath = saveFileInfo.filePath
val dirPath = FileManagerUtil.absoluteRootPath(saveFileInfo.dirPath)
val fileName = saveFileInfo.fileName
val content = saveFileInfo.content
val filePath = "$dirPath${File.separator}$fileName"
call.respond(SaveFileAction.saveRes(content, filePath))
call.respond(SaveFileAction.saveFileRes(content, filePath))
}
/**
......@@ -187,9 +186,8 @@ val DoKitFileRouter: Application.() -> Unit = {
get("/getAllTable") {
val queryParameters = call.request.queryParameters
val dirPath = queryParameters["dirPath"]
val dirPath = FileManagerUtil.absoluteRootPath(queryParameters["dirPath"])
val fileName = queryParameters["fileName"]
val fileType = queryParameters["fileType"]
val filePath = "$dirPath${File.separator}$fileName"
call.respond(DatabaseAction.allTablesRes(filePath))
}
......@@ -199,7 +197,7 @@ val DoKitFileRouter: Application.() -> Unit = {
*/
get("/getTableData") {
val queryParameters = call.request.queryParameters
val dirPath = queryParameters["dirPath"]
val dirPath = FileManagerUtil.absoluteRootPath(queryParameters["dirPath"])
val fileName = queryParameters["fileName"]
val tableName = queryParameters["tableName"]
val filePath = "$dirPath${File.separator}$fileName"
......@@ -211,7 +209,7 @@ val DoKitFileRouter: Application.() -> Unit = {
*/
post("/insertRow") {
val rowRequestInfo = call.receive<RowRequestInfo>()
val dirPath = rowRequestInfo.dirPath
val dirPath = FileManagerUtil.absoluteRootPath(rowRequestInfo.dirPath)
val fileName = rowRequestInfo.fileName
val tableName = rowRequestInfo.tableName
val filePath = "$dirPath${File.separator}$fileName"
......@@ -224,7 +222,7 @@ val DoKitFileRouter: Application.() -> Unit = {
*/
post("/updateRow") {
val rowRequestInfo = call.receive<RowRequestInfo>()
val dirPath = rowRequestInfo.dirPath
val dirPath = FileManagerUtil.absoluteRootPath(rowRequestInfo.dirPath)
val fileName = rowRequestInfo.fileName
val tableName = rowRequestInfo.tableName
val filePath = "$dirPath${File.separator}$fileName"
......@@ -237,7 +235,7 @@ val DoKitFileRouter: Application.() -> Unit = {
*/
post("/deleteRow") {
val rowRequestInfo = call.receive<RowRequestInfo>()
val dirPath = rowRequestInfo.dirPath
val dirPath = FileManagerUtil.absoluteRootPath(rowRequestInfo.dirPath)
val fileName = rowRequestInfo.fileName
val tableName = rowRequestInfo.tableName
val filePath = "$dirPath${File.separator}$fileName"
......
package com.didichuxing.doraemonkit.kit.filemanager
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.PathUtils
/**
* ================================================
* 作 者:jint(金台)
* 版 本:1.0
* 创建日期:2020/6/29-17:10
* 描 述:
* 修订历史:
* ================================================
*/
object FileManagerUtil {
private val internalAppRootPath by lazy { PathUtils.getInternalAppDataPath() }
private val internalAppRootReplacePath by lazy { FileUtils.getFileName(PathUtils.getInternalAppDataPath()) }
private val externalStorageRootPath by lazy { PathUtils.getExternalStoragePath() }
private val externalStorageRootReplacePath by lazy { "external" }
/**
* 输出相对路径
*/
fun relativeRootPath(path: String?): String {
path?.let {
if (it.contains(internalAppRootPath)) {
return it.replace(internalAppRootPath, internalAppRootReplacePath)
} else if (it.contains(externalStorageRootPath)) {
return it.replace(externalStorageRootPath, externalStorageRootReplacePath)
}
return it
}
return ""
}
/**
* 合并绝对路径
*/
fun absoluteRootPath(path: String?): String {
path?.let {
if (it.contains(internalAppRootReplacePath)) {
return it.replace(internalAppRootReplacePath, internalAppRootPath)
} else if (it.contains(externalStorageRootReplacePath)) {
return it.replace(externalStorageRootReplacePath, externalStorageRootPath)
}
return it
}
return ""
}
}
\ No newline at end of file
......@@ -13,7 +13,7 @@ import java.io.File
* ================================================
*/
object DeleteFileAction {
fun createDeleteRes(filePath: String, dirPath: String, fileName: String): MutableMap<String, Any> {
fun deleteFileRes(filePath: String, dirPath: String, fileName: String): MutableMap<String, Any> {
val response = mutableMapOf<String, Any>()
//删除文件夹
if (FileUtils.isFileExists(filePath)) {
......
......@@ -12,7 +12,7 @@ import com.blankj.utilcode.util.DeviceUtils
* ================================================
*/
object DeviceInfoAction {
fun createDeviceInfo(): MutableMap<String, Any> {
fun deviceInfoRes(): MutableMap<String, Any> {
return mutableMapOf<String, Any>().apply {
this["code"] = 200
val data = mutableMapOf<String, String>().apply {
......
......@@ -13,7 +13,7 @@ import com.blankj.utilcode.util.FileUtils
* ================================================
*/
object FileDetailAction {
fun createFileDetailInfo(filePath: String, fileType: String?): MutableMap<String, Any> {
fun fileDetailInfoRes(filePath: String, fileType: String?): MutableMap<String, Any> {
val params = mutableMapOf<String, Any>()
if (FileUtils.isFileExists(filePath)) {
params["code"] = 200
......
......@@ -2,6 +2,7 @@ package com.didichuxing.doraemonkit.kit.filemanager.action.file
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.PathUtils
import com.didichuxing.doraemonkit.kit.filemanager.FileManagerUtil
import java.io.File
/**
......@@ -14,21 +15,21 @@ import java.io.File
* ================================================
*/
object FileListAction {
fun createFileList(filePath: String): MutableMap<String, Any> {
fun fileListRes(filePath: String): MutableMap<String, Any> {
//root path
val params = mutableMapOf<String, Any>().apply {
this["code"] = 200
}
if (filePath == "/") {
val data = mutableMapOf<String, Any>().apply {
this["filePath"] = "/"
this["dirPath"] = "/"
this["fileList"] = createRootInfo()
}
params["data"] = data
} else {
//not root path
val data = mutableMapOf<String, Any>().apply {
this["filePath"] = filePath
this["dirPath"] = FileManagerUtil.relativeRootPath(filePath)
this["fileList"] = traverseDir(filePath)
}
params["data"] = data
......@@ -45,8 +46,8 @@ object FileListAction {
val fileInfos = mutableListOf<FileInfo>()
val internalAppDataPath = PathUtils.getInternalAppDataPath()
val externalStoragePath = PathUtils.getExternalStoragePath()
fileInfos.add(FileInfo(internalAppDataPath, FileUtils.getFileName(internalAppDataPath), "folder", "", "" + FileUtils.getFileLastModified(internalAppDataPath), true))
fileInfos.add(FileInfo(externalStoragePath, FileUtils.getFileName(externalStoragePath), "folder", "", "" + FileUtils.getFileLastModified(externalStoragePath), true))
fileInfos.add(FileInfo(FileManagerUtil.relativeRootPath(internalAppDataPath), FileUtils.getFileName(internalAppDataPath), "folder", "", "" + FileUtils.getFileLastModified(internalAppDataPath), true))
fileInfos.add(FileInfo(FileManagerUtil.relativeRootPath(externalStoragePath), "external", "folder", "", "" + FileUtils.getFileLastModified(externalStoragePath), true))
return fileInfos
}
......@@ -58,8 +59,10 @@ object FileListAction {
val dir = File(dirPath)
if (FileUtils.isDir(dir)) {
dir.listFiles()?.forEach { file ->
val fileInfo = FileInfo(file.path, file.name, if (FileUtils.isDir(file)) {
val fileInfo = FileInfo(FileManagerUtil.relativeRootPath(file.path), file.name, if (FileUtils.isDir(file)) {
"folder"
} else if (dir.absolutePath.contains("/databases")) {
"db"
} else {
FileUtils.getFileExtension(file)
}, "", "" + FileUtils.getFileLastModified(file), false)
......@@ -73,7 +76,7 @@ object FileListAction {
data class FileInfo(
val filePath: String,
val dirPath: String,
val fileName: String,
val fileType: String,
val fileUri: String,
......
......@@ -10,7 +10,7 @@ package com.didichuxing.doraemonkit.kit.filemanager.action.file
* ================================================
*/
object IndexAction {
fun createIndexInfo(): MutableMap<String, Any> {
fun indexInfoRes(): MutableMap<String, Any> {
return mutableMapOf<String, Any>().apply {
this["code"] = 200
this["data"] = "请在www.dokit.cn里的控制台中的文件同步助手中使用该功能"
......
......@@ -12,7 +12,7 @@ import com.blankj.utilcode.util.FileUtils
* ================================================
*/
object RenameFileAction {
fun renameRes(newName: String, filePath: String): MutableMap<String, Any> {
fun renameFileRes(newName: String, filePath: String): MutableMap<String, Any> {
val response = mutableMapOf<String, Any>()
if (FileUtils.isFileExists(filePath)) {
FileUtils.rename(filePath, newName)
......
......@@ -13,7 +13,7 @@ import com.blankj.utilcode.util.FileUtils
* ================================================
*/
object SaveFileAction {
fun saveRes(content: String, filePath: String): MutableMap<String, Any> {
fun saveFileRes(content: String, filePath: String): MutableMap<String, Any> {
val response = mutableMapOf<String, Any>()
if (FileUtils.isFileExists(filePath)) {
FileIOUtils.writeFileFromString(filePath, content, false)
......
package com.didichuxing.doraemonkit.kit.filemanager.action.file
import com.blankj.utilcode.util.FileUtils
import com.didichuxing.doraemonkit.kit.filemanager.FileManagerUtil
import io.ktor.http.content.MultiPartData
import io.ktor.http.content.PartData
import io.ktor.http.content.forEachPart
......@@ -34,7 +35,7 @@ object UploadFileAction {
val response = mutableMapOf<String, Any>()
filePart?.let {
val dirPath = formPart?.value
val dirPath = FileManagerUtil.absoluteRootPath(formPart?.value)
val fileName = filePart?.originalFileName
val file = File("$dirPath${File.separator}$fileName")
......
......@@ -9,5 +9,5 @@ package com.didichuxing.doraemonkit.kit.filemanager.bean
* 修订历史:
* ================================================
*/
data class DirInfo(val filePath: String, val fileName: String) {
data class DirInfo(val dirPath: String, val fileName: String) {
}
\ No newline at end of file
......@@ -9,4 +9,4 @@ package com.didichuxing.doraemonkit.kit.filemanager.bean
* 修订历史:
* ================================================
*/
data class RenameFileInfo(val filePath: String, val oldName: String, val newName: String)
\ No newline at end of file
data class RenameFileInfo(val dirPath: String, val oldName: String, val newName: String)
\ No newline at end of file
......@@ -9,4 +9,4 @@ package com.didichuxing.doraemonkit.kit.filemanager.bean
* 修订历史:
* ================================================
*/
data class SaveFileInfo(val filePath: String, val fileName: String, val content: String)
\ No newline at end of file
data class SaveFileInfo(val dirPath: String, val fileName: String, val content: String)
\ No newline at end of file
......@@ -24,12 +24,12 @@ object DBManager {
private val sqliteDBs: MutableMap<String, SQLiteDB> = mutableMapOf()
private fun openDB(dbFactory: DBFactory, databasePath: String, password: String?): SQLiteDB {
private fun openDB(dbFactory: DBFactory, databasePath: String, password: String?): SQLiteDB? {
return if (sqliteDBs.containsKey(databasePath)) {
sqliteDBs["databasePath"]!!
sqliteDBs["databasePath"]
} else {
sqliteDBs["databasePath"] = dbFactory.create(DoraemonKit.APPLICATION!!.applicationContext, databasePath, password)
sqliteDBs["databasePath"]!!
sqliteDBs["databasePath"]
}
}
......@@ -40,16 +40,18 @@ object DBManager {
fun getAllTableName(databasePath: String, password: String?): List<String> {
val openDB = openDB(NormalDBFactory(), databasePath, password)
val tables = mutableListOf<String>()
val cursor = openDB.rawQuery("SELECT name FROM sqlite_master WHERE type='table' OR type='view' ORDER BY name COLLATE NOCASE", null)
cursor?.let {
if (it.moveToFirst()) {
while (!it.isAfterLast) {
val name = it.getString(0)
tables.add(name)
it.moveToNext()
openDB?.let { db ->
val cursor = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table' OR type='view' ORDER BY name COLLATE NOCASE", null)
cursor?.let {
if (it.moveToFirst()) {
while (!it.isAfterLast) {
val name = it.getString(0)
tables.add(name)
it.moveToNext()
}
}
it.close()
}
it.close()
}
return tables
......@@ -60,31 +62,39 @@ object DBManager {
*/
fun getTableData(databasePath: String, password: String?, tableName: String): Map<String, Any> {
val openDB = openDB(NormalDBFactory(), databasePath, password)
val tableFieldInfos = getTableFieldInfos(openDB, tableName)
val tableRows = getTableRows(openDB, tableName, tableFieldInfos)
val params = mutableMapOf<String, Any>()
params["fieldInfo"] = tableFieldInfos
params["rows"] = tableRows
openDB?.let { db ->
val tableFieldInfos = getTableFieldInfos(db, tableName)
val tableRows = getTableRows(db, tableName, tableFieldInfos)
params["fieldInfo"] = tableFieldInfos
params["rows"] = tableRows
}
return params
}
/**
* 插入数据
*/
fun insertRow(databasePath: String, password: String?, tableName: String, rowDatas: List<RowFiledInfo>): Long {
val sqlite = openDB(NormalDBFactory(), databasePath, password)
val openDB = openDB(NormalDBFactory(), databasePath, password)
if (rowDatas.isEmpty()) {
return -1
}
val contentValues = ContentValues()
rowDatas.forEach { rowInfo ->
if (rowInfo.value.isNullOrBlank()) {
contentValues.put(rowInfo.title, "null")
} else {
contentValues.put(rowInfo.title, rowInfo.value)
openDB?.let { db ->
val contentValues = ContentValues()
rowDatas.forEach { rowInfo ->
if (rowInfo.value.isNullOrBlank()) {
contentValues.put(rowInfo.title, "null")
} else {
contentValues.put(rowInfo.title, rowInfo.value)
}
}
return db.insert("[$tableName]", null, contentValues)
}
return sqlite.insert("[$tableName]", null, contentValues)
return -1
}
......@@ -92,67 +102,75 @@ object DBManager {
* 更新数据
*/
fun updateRow(databasePath: String, password: String?, tableName: String, rowDatas: List<RowFiledInfo>): Int {
val sqlite = openDB(NormalDBFactory(), databasePath, password)
val openDB = openDB(NormalDBFactory(), databasePath, password)
if (rowDatas.isEmpty()) {
return -1
}
val contentValues = ContentValues()
var whereClause = ""
val whereArgList = mutableListOf<String>()
rowDatas.forEach { rowInfo ->
if (rowInfo.isPrimary) {
if (whereClause.isBlank()) {
whereClause = "${rowInfo.title} =? "
} else {
whereClause = "$whereClause and ${rowInfo.title} =? "
}
whereArgList.add(if (rowInfo.value.isNullOrBlank()) {
"null"
} else {
rowInfo.value
})
} else {
if (rowInfo.value.isNullOrBlank()) {
contentValues.put(rowInfo.title, "null")
openDB?.let { db ->
val contentValues = ContentValues()
var whereClause = ""
val whereArgList = mutableListOf<String>()
rowDatas.forEach { rowInfo ->
if (rowInfo.isPrimary) {
if (whereClause.isBlank()) {
whereClause = "${rowInfo.title} =? "
} else {
whereClause = "$whereClause and ${rowInfo.title} =? "
}
whereArgList.add(if (rowInfo.value.isNullOrBlank()) {
"null"
} else {
rowInfo.value
})
} else {
contentValues.put(rowInfo.title, rowInfo.value)
if (rowInfo.value.isNullOrBlank()) {
contentValues.put(rowInfo.title, "null")
} else {
contentValues.put(rowInfo.title, rowInfo.value)
}
}
}
val whereArgs = whereArgList.toTypedArray()
return db.update("[$tableName]", contentValues, whereClause, whereArgs)
}
val whereArgs = whereArgList.toTypedArray()
return sqlite.update("[$tableName]", contentValues, whereClause, whereArgs)
return -1
}
/**
* 删除数据
*/
fun deleteRow(databasePath: String, password: String?, tableName: String, rowDatas: List<RowFiledInfo>): Int {
val sqlite = openDB(NormalDBFactory(), databasePath, password)
val openDB = openDB(NormalDBFactory(), databasePath, password)
if (rowDatas.isEmpty()) {
return -1
}
var whereClause = ""
val whereArgList = mutableListOf<String>()
rowDatas.forEach { rowInfo ->
if (rowInfo.isPrimary) {
if (whereClause.isBlank()) {
whereClause = "${rowInfo.title} =? "
} else {
whereClause = "$whereClause and ${rowInfo.title} =? "
openDB?.let { db ->
var whereClause = ""
val whereArgList = mutableListOf<String>()
rowDatas.forEach { rowInfo ->
if (rowInfo.isPrimary) {
if (whereClause.isBlank()) {
whereClause = "${rowInfo.title} =? "
} else {
whereClause = "$whereClause and ${rowInfo.title} =? "
}
whereArgList.add(if (rowInfo.value.isNullOrBlank()) {
"null"
} else {
rowInfo.value
})
}
whereArgList.add(if (rowInfo.value.isNullOrBlank()) {
"null"
} else {
rowInfo.value
})
}
val whereArgs = whereArgList.toTypedArray()
return db.delete("[$tableName]", whereClause, whereArgs)
}
val whereArgs = whereArgList.toTypedArray()
return sqlite.delete("[$tableName]", whereClause, whereArgs)
return -1
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册