mirror of
https://github.com/KOHGYLW/kiftd-source.git
synced 2025-01-07 03:26:57 +08:00
优化了删除逻辑
This commit is contained in:
parent
d1206188dd
commit
6584b1908f
@ -48,11 +48,6 @@
|
||||
#{fileId,jdbcType=VARCHAR}
|
||||
</update>
|
||||
|
||||
<delete id="deleteByParentFolderId" parameterType="java.lang.String">
|
||||
DELETE FROM
|
||||
FILE WHERE file_parent_folder = #{pfid,jdbcType=VARCHAR}
|
||||
</delete>
|
||||
|
||||
<delete id="deleteById" parameterType="java.lang.String">
|
||||
DELETE FROM FILE WHERE
|
||||
file_id = #{fileId,jdbcType=VARCHAR}
|
||||
|
@ -50,8 +50,6 @@ public interface NodeMapper {
|
||||
|
||||
int update(final Node f);
|
||||
|
||||
int deleteByParentFolderId(final String pfid);
|
||||
|
||||
int deleteById(final String fileId);
|
||||
|
||||
Node queryById(final String fileId);
|
||||
|
@ -300,13 +300,10 @@ public class FileServiceImpl extends RangeFileStreamWriter implements FileServic
|
||||
|| !ConfigureReader.instance().accessFolder(f, account)) {
|
||||
return NO_AUTHORIZED;
|
||||
}
|
||||
// 从节点删除
|
||||
if (this.fm.deleteById(fileId) >= 0) {
|
||||
// 从文件块删除
|
||||
if (this.fbu.deleteFromFileBlocks(node)) {
|
||||
this.lu.writeDeleteFileEvent(request, node);
|
||||
return "deleteFileSuccess";
|
||||
}
|
||||
// 删除文件节点
|
||||
if (this.fbu.deleteNode(node)) {
|
||||
this.lu.writeDeleteFileEvent(request, node);
|
||||
return "deleteFileSuccess";
|
||||
}
|
||||
return "cannotDeleteFile";
|
||||
}
|
||||
@ -420,11 +417,7 @@ public class FileServiceImpl extends RangeFileStreamWriter implements FileServic
|
||||
return NO_AUTHORIZED;
|
||||
}
|
||||
// 删除文件节点
|
||||
if (this.fm.deleteById(fileId) <= 0) {
|
||||
return "cannotDeleteFile";
|
||||
}
|
||||
// 删除文件块
|
||||
if (!this.fbu.deleteFromFileBlocks(file)) {
|
||||
if (!this.fbu.deleteNode(file)) {
|
||||
return "cannotDeleteFile";
|
||||
}
|
||||
// 日志记录
|
||||
@ -672,7 +665,16 @@ public class FileServiceImpl extends RangeFileStreamWriter implements FileServic
|
||||
this.lu.writeMoveFileEvent(account, ip, originPath, fbu.getNodePath(node), isCopy);
|
||||
}
|
||||
// 最后,尝试删除冲突节点的文件块。注意:该操作必须在复制节点插入后再执行!
|
||||
fbu.deleteFromFileBlocks(n);
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("path", n.getFilePath());
|
||||
map.put("fileId", n.getFileId());
|
||||
List<Node> nodes = fm.queryByPathExcludeById(map);
|
||||
if (nodes == null || nodes.isEmpty()) {
|
||||
File file = fbu.getFileFromBlocks(n);
|
||||
if (file != null) {
|
||||
file.delete();// 此处无需再判断是否成功
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 如果原节点删除失败,则操作失败
|
||||
return "cannotMoveFiles";
|
||||
|
@ -140,7 +140,7 @@ public class FileBlockUtil {
|
||||
// 因其他原因生成失败也返回null
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* <h2>将新上传的文件存入文件系统</h2>
|
||||
@ -285,33 +285,42 @@ public class FileBlockUtil {
|
||||
|
||||
/**
|
||||
*
|
||||
* <h2>删除文件系统中的一个文件块</h2>
|
||||
* <h2>删除文件系统中的一个文件节点,同时清理文件块</h2>
|
||||
* <p>
|
||||
* 根据传入的文件节点对象,删除其在文件系统中保存的对应文件块。仅当传入文件节点所对应的文件块不再有其他节点引用时
|
||||
* 才会真的进行删除操作,否则直接返回true。
|
||||
* 删除传入的文件节点,之后判断是否需要删除其在文件系统中保存的对应文件块,若该文件节点所对应的文件块不再有其他节点引用,
|
||||
* 则进行删除操作,否则直接返回true。
|
||||
* </p>
|
||||
*
|
||||
* @author 青阳龙野(kohgylw)
|
||||
* @param f kohgylw.kiftd.server.model.Node 要删除的文件节点对象
|
||||
* @return boolean 删除结果,true为成功
|
||||
* @return boolean 删除结果,true为成功,否则返回false。若传入节点为null,也会返回false
|
||||
*/
|
||||
public boolean deleteFromFileBlocks(Node f) {
|
||||
// 检查是否还有其他节点引用相同的文件块
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("path", f.getFilePath());
|
||||
map.put("fileId", f.getFileId());
|
||||
List<Node> nodes = fm.queryByPathExcludeById(map);
|
||||
if (nodes == null || nodes.isEmpty()) {
|
||||
// 如果已经无任何节点再引用此文件块,则删除它
|
||||
File file = getFileFromBlocks(f);// 获取对应的文件块对象
|
||||
if (file != null) {
|
||||
return file.delete();// 执行删除操作
|
||||
public boolean deleteNode(Node f) {
|
||||
if (f != null) {
|
||||
if (fm.deleteById(f.getFileId()) > 0) {
|
||||
// 检查是否还有其他节点引用相同的文件块
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("path", f.getFilePath());
|
||||
map.put("fileId", f.getFileId());
|
||||
List<Node> nodes = fm.queryByPathExcludeById(map);
|
||||
if (nodes == null || nodes.isEmpty()) {
|
||||
// 如果已经无任何节点再引用此文件块,则删除它
|
||||
File file = getFileFromBlocks(f);// 获取对应的文件块对象
|
||||
if (file != null) {
|
||||
if (file.delete()) {
|
||||
return true;// 文件块被删除,认为删除成功
|
||||
} else {
|
||||
// 文件块无法删除,尝试回滚节点数据
|
||||
if (file.exists() && fm.insert(f) > 0) {
|
||||
return false;// 回滚成功,认为删除失败
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;// 如果文件块仍被其他节点引用,或是已无此文件块,或是文件块无法删除且节点回滚失败,则认为删除成功
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
// 如果还有,那么直接返回true即可,认为此节点的文件块已经删除了(其他的引用是属于其他节点的)
|
||||
return true;
|
||||
}
|
||||
return false;// 若节点删除失败,或是节点为null,则返回false
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -73,13 +73,6 @@ public class FileNodeUtil {
|
||||
state2.execute(
|
||||
"CREATE TABLE IF NOT EXISTS FILE(file_id VARCHAR(128) PRIMARY KEY,file_name VARCHAR(128) NOT NULL,file_size VARCHAR(128) NOT NULL,file_parent_folder varchar(128) NOT NULL,file_creation_date varchar(128) NOT NULL,file_creator varchar(128) NOT NULL,file_path varchar(128) NOT NULL)");
|
||||
state2.close();
|
||||
// 为了匹配之前的版本而设计的兼容性字段设置,后续可能会删除
|
||||
if (!ConfigureReader.instance().useMySQL()) {
|
||||
final Statement state3 = conn.createStatement();
|
||||
state3.execute(
|
||||
"ALTER TABLE FOLDER ADD COLUMN IF NOT EXISTS folder_constraint INT NOT NULL DEFAULT 0");
|
||||
state3.close();
|
||||
}
|
||||
// 为数据库生成索引,此处分为MySQL和H2两种操作
|
||||
if (ConfigureReader.instance().useMySQL()) {
|
||||
final Statement state4 = conn.createStatement();
|
||||
|
@ -29,8 +29,7 @@ public class FolderUtil {
|
||||
* </p>
|
||||
*
|
||||
* @author 青阳龙野(kohgylw)
|
||||
* @param fid
|
||||
* java.lang.String 要获取的目标文件夹ID
|
||||
* @param fid java.lang.String 要获取的目标文件夹ID
|
||||
* @return java.util.List
|
||||
* 指定文件夹的所有父级文件夹列表,以kohgylw.kiftd.server.model.Folder形式封装。
|
||||
*/
|
||||
@ -62,8 +61,7 @@ public class FolderUtil {
|
||||
* </p>
|
||||
*
|
||||
* @author 青阳龙野(kohgylw)
|
||||
* @param folderId
|
||||
* java.lang.String 要删除的文件夹树的ID,不能为null。
|
||||
* @param folderId java.lang.String 要删除的文件夹树的ID,不能为null。
|
||||
*/
|
||||
public void deleteAllChildFolder(final String folderId) {
|
||||
final Thread deleteChildFolderThread = new Thread(() -> this.iterationDeleteFolder(folderId));
|
||||
@ -72,17 +70,12 @@ public class FolderUtil {
|
||||
|
||||
private void iterationDeleteFolder(final String folderId) {
|
||||
final List<Folder> cf = (List<Folder>) this.fm.queryByParentId(folderId);
|
||||
if (cf.size() > 0) {
|
||||
for (final Folder f : cf) {
|
||||
this.iterationDeleteFolder(f.getFolderId());
|
||||
}
|
||||
for (final Folder f : cf) {
|
||||
this.iterationDeleteFolder(f.getFolderId());
|
||||
}
|
||||
final List<Node> files = (List<Node>) this.fim.queryByParentFolderId(folderId);
|
||||
if (files.size() > 0) {
|
||||
this.fim.deleteByParentFolderId(folderId);
|
||||
for (final Node f2 : files) {
|
||||
this.fbu.deleteFromFileBlocks(f2);
|
||||
}
|
||||
for (final Node f2 : files) {
|
||||
this.fbu.deleteNode(f2);
|
||||
}
|
||||
this.fm.deleteById(folderId);
|
||||
}
|
||||
@ -167,8 +160,7 @@ public class FolderUtil {
|
||||
* </p>
|
||||
*
|
||||
* @author 青阳龙野(kohgylw)
|
||||
* @param f
|
||||
* kohgylw.kiftd.server.model.Folder 要检查的文件夹对象
|
||||
* @param f kohgylw.kiftd.server.model.Folder 要检查的文件夹对象
|
||||
* @return boolean 是否有效,若返回false则进行了数据回滚
|
||||
*/
|
||||
public boolean isValidFolder(Folder f) {
|
||||
@ -195,14 +187,11 @@ public class FolderUtil {
|
||||
* </p>
|
||||
*
|
||||
* @author 青阳龙野(kohgylw)
|
||||
* @param prototype
|
||||
* kohgylw.kiftd.server.model.Folder 要复制的目标文件夹,即复制的样板
|
||||
* @param parentFolder
|
||||
* kohgylw.kiftd.server.model.Folder 复制文件夹的父文件夹,指定在哪个路径下创建目标文件夹的副本
|
||||
* @param newName
|
||||
* java.lang.String 副本文件夹的新名称,以覆盖原本的名称,如果传入null则仍使用原名
|
||||
* @param excludeFolderId
|
||||
* java.lang.String 这个参数是方便后续迭代时避免循环拷贝的,首次调用必须传入null!
|
||||
* @param prototype kohgylw.kiftd.server.model.Folder 要复制的目标文件夹,即复制的样板
|
||||
* @param parentFolder kohgylw.kiftd.server.model.Folder
|
||||
* 复制文件夹的父文件夹,指定在哪个路径下创建目标文件夹的副本
|
||||
* @param newName java.lang.String 副本文件夹的新名称,以覆盖原本的名称,如果传入null则仍使用原名
|
||||
* @param excludeFolderId java.lang.String 这个参数是方便后续迭代时避免循环拷贝的,首次调用必须传入null!
|
||||
* @return kohgylw.kiftd.server.model.Folder 完整复制成功则返回复制好的文件夹对象,
|
||||
* 否则返回null(包括传入目标文件夹或父文件夹参数错误的情况)
|
||||
*/
|
||||
@ -265,12 +254,10 @@ public class FolderUtil {
|
||||
* </p>
|
||||
*
|
||||
* @author 青阳龙野(kohgylw)
|
||||
* @param prototype
|
||||
* kohgylw.kiftd.server.model.Folder 要复制的目标文件夹,即复制的样板
|
||||
* @param parentFolder
|
||||
* kohgylw.kiftd.server.model.Folder 复制文件夹的父文件夹,指定在哪个路径下创建目标文件夹的副本
|
||||
* @param newName
|
||||
* java.lang.String 副本文件夹的新名称,以覆盖原本的名称,如果传入null则仍使用原名
|
||||
* @param prototype kohgylw.kiftd.server.model.Folder 要复制的目标文件夹,即复制的样板
|
||||
* @param parentFolder kohgylw.kiftd.server.model.Folder
|
||||
* 复制文件夹的父文件夹,指定在哪个路径下创建目标文件夹的副本
|
||||
* @param newName java.lang.String 副本文件夹的新名称,以覆盖原本的名称,如果传入null则仍使用原名
|
||||
* @return kohgylw.kiftd.server.model.Folder 完整复制成功则返回复制好的文件夹对象,
|
||||
* 否则返回null(包括传入目标文件夹或父文件夹参数错误的情况)
|
||||
*/
|
||||
@ -286,8 +273,7 @@ public class FolderUtil {
|
||||
* </p>
|
||||
*
|
||||
* @author 青阳龙野(kohgylw)
|
||||
* @param f
|
||||
* kohgylw.kiftd.server.model.Folder 要获取路径的文件夹
|
||||
* @param f kohgylw.kiftd.server.model.Folder 要获取路径的文件夹
|
||||
* @return java.lang.String 指定节点的逻辑路径,包含其自身完整的文件夹路径名,各级之间以“/”分割。
|
||||
*/
|
||||
public String getFolderPath(Folder f) {
|
||||
@ -309,10 +295,8 @@ public class FolderUtil {
|
||||
* </p>
|
||||
*
|
||||
* @author 青阳龙野(kohgylw)
|
||||
* @param folderId
|
||||
* 要修改的文件夹ID
|
||||
* @param c
|
||||
* 约束等级
|
||||
* @param folderId 要修改的文件夹ID
|
||||
* @param c 约束等级
|
||||
*/
|
||||
public void changeChildFolderConstraint(String folderId, int c) {
|
||||
List<Folder> cfs = fm.queryByParentId(folderId);
|
||||
|
@ -1454,14 +1454,11 @@ public class KiftdWebDAVServlet extends HttpServlet {
|
||||
return;
|
||||
}
|
||||
// 删除文件节点
|
||||
if (this.nm.deleteById(node.getFileId()) >= 0) {
|
||||
// 删除文件块
|
||||
if (this.fbu.deleteFromFileBlocks(node)) {
|
||||
// 删除成功,记录日志并返回状态码204
|
||||
this.lu.writeDeleteFileEvent(req, node);
|
||||
resp.setStatus(WebdavStatus.SC_NO_CONTENT);
|
||||
return;
|
||||
}
|
||||
if (this.fbu.deleteNode(node)) {
|
||||
// 删除成功,记录日志并返回状态码204
|
||||
this.lu.writeDeleteFileEvent(req, node);
|
||||
resp.setStatus(WebdavStatus.SC_NO_CONTENT);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// 删除失败,返回状态码500
|
||||
@ -1678,7 +1675,16 @@ public class KiftdWebDAVServlet extends HttpServlet {
|
||||
// 返回状态码204
|
||||
resp.setStatus(WebdavStatus.SC_NO_CONTENT);
|
||||
// 删除冲突节点的文件块
|
||||
fbu.deleteFromFileBlocks(conflictNode);
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("path", conflictNode.getFilePath());
|
||||
map.put("fileId", conflictNode.getFileId());
|
||||
List<kohgylw.kiftd.server.model.Node> nodes = nm.queryByPathExcludeById(map);
|
||||
if (nodes == null || nodes.isEmpty()) {
|
||||
File file = fbu.getFileFromBlocks(conflictNode);
|
||||
if (file != null) {
|
||||
file.delete();// 此处无需再判断是否成功
|
||||
}
|
||||
}
|
||||
// 成功
|
||||
return;
|
||||
}
|
||||
@ -1700,7 +1706,16 @@ public class KiftdWebDAVServlet extends HttpServlet {
|
||||
// 返回状态码204
|
||||
resp.setStatus(WebdavStatus.SC_NO_CONTENT);
|
||||
// 删除冲突节点的文件块
|
||||
fbu.deleteFromFileBlocks(conflictNode);
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("path", conflictNode.getFilePath());
|
||||
map.put("fileId", conflictNode.getFileId());
|
||||
List<kohgylw.kiftd.server.model.Node> nodes = nm.queryByPathExcludeById(map);
|
||||
if (nodes == null || nodes.isEmpty()) {
|
||||
File file = fbu.getFileFromBlocks(conflictNode);
|
||||
if (file != null) {
|
||||
file.delete();// 此处无需再判断是否成功
|
||||
}
|
||||
}
|
||||
// 成功
|
||||
return;
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
#Generated by Maven Integration for Eclipse
|
||||
#Wed Jun 08 09:49:22 CST 2022
|
||||
#Fri Jun 10 07:40:22 CST 2022
|
||||
m2e.projectLocation=/Users/kohgylw/program/java-workspace/kiftd
|
||||
m2e.projectName=kiftd
|
||||
groupId=kohgylw
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
target/classes/kohgylw/kiftd/server/util/ConfigureReader$1.class
Normal file
BIN
target/classes/kohgylw/kiftd/server/util/ConfigureReader$1.class
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
target/classes/kohgylw/kiftd/server/util/KiftdProperties$1.class
Normal file
BIN
target/classes/kohgylw/kiftd/server/util/KiftdProperties$1.class
Normal file
Binary file not shown.
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user