当前位置:网站首页>文件 - 07 删除文件: 根据fileIds批量删除文件及文件信息
文件 - 07 删除文件: 根据fileIds批量删除文件及文件信息
2022-07-31 05:48:00 【追风筝~】
系列文章:
文件 - 01 上传附件到服务器并保存信息到MySQL
文件 - 02 上传临时文件到服务器
文件 - 03 根据文件id获取下载链接
文件 - 04 根据文件下载链接下载文件
文件 - 05 根据文件Id下载文件
文件 - 06 根据文件id/图片id回显给前端
附件表:
CREATE TABLE `t_upload_file` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`fileId` varchar(50) NOT NULL COMMENT '文件id',
`fileName` varchar(50) NOT NULL COMMENT '文件名',
`fileType` varchar(100) NOT NULL COMMENT '文件类型',
`fileSize` int(10) NOT NULL COMMENT '文件大小bytes',
`filePathSuffix` varchar(60) NOT NULL COMMENT '文件存储路径',
`fileUploader` varchar(32) DEFAULT NULL,
`uploadTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '文件上传时间',
`lastUsedTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '文件最近使用时间',
`neverExpire` tinyint(1) NOT NULL DEFAULT '1' COMMENT '文件是否永不过期,当为1时,则忽略expireTime',
`expireTime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '文件过期时间',
PRIMARY KEY (`id`),
UNIQUE KEY `fileId` (`fileId`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
1. FileController
@RestController
@ResponseBody
@ResponseResult
@Slf4j
@RequestMapping("/ngsoc/PORTAL/api/v1")
public class FileController {
@ApiOperation(value = "根据fileIds批量删除文件", notes = "根据fileIds批量删除文件及文件信息", httpMethod = "DELETE")
@DeleteMapping(value = "/files")
public void deleteFiles(
@ApiParam(value = "文件名列表", required = true) @RequestBody List<String> fileIds
) {
fileService.deleteFileByIds(fileIds);
}
}
2. FileServiceImpl
public interface IFileService {
/** * 根据文件id列表批量删除文件 * * @param fileIds 文件id列表 */
void deleteFileByIds(List<String> fileIds);
}
@Controller
@Slf4j
public class FileServiceImpl implements IFileService {
@Autowired
private IFileMapper fileDao;
@Autowired
private FileConfig fileConfig;
@Autowired
private RedisTemplate<String, String> stringRedisTemplate;
@Setter(onMethod_ = @Autowired)
private FileIdAuthorityServiceImpl fileIdAuthorityServiceImpl;
@Override
public void deleteFileByIds(List<String> fileIds) {
for (String fileId : fileIds) {
deleteFileById(fileId, true);
}
}
@Override
public void deleteFileById(String fileId, boolean checkAuth) {
// 删除文件 1. getFileInfo --> path --> delete file
// 2. clear mysql info
// 3. redis will expire automatically
// 4. 如果期间有根据token/id下载 就会出错
FileInfo fileInfo = this.getFileInfo(fileId);
if (fileInfo == null) {
return;
}
if (checkAuth) {
fileIdAuthorityServiceImpl.assertThisUserHaveAuthority(fileInfo);
}
String pathSuffix = fileInfo.getFilePathSuffix();
String storePath = fileConfig.getSavePath().concat(pathSuffix);
// 刪除服务器保存的文件
// 如果文件已经不存在,返回True
boolean delSucceed = FileUtil.del(storePath);
if (delSucceed) {
// 删除文件信息
fileDao.deleteFileInfo(fileId);
}
}
}
3. IFileMapper
@Mapper
@Repository
public interface IFileMapper {
/** * 根据fileId删除文件信息 * * @param fileId 文件id */
void deleteFileInfo(@Param("fileId") String fileId);
}
<delete id="deleteFileInfo" parameterType="String">
delete
from t_upload_file
where fileId = #{fileId} and <include refid="unexpiredFilter"/>
</delete>
边栏推荐
- 安装和使用uView
- MySQL的触发器
- 剑指offer(一)
- R——避免使用 col=0
- 【Star项目】小帽飞机大战(七)
- Run the NPM will pop up to ask "how are you going to open this file?"
- 第三方库-store
- 【编程题】【Scratch三级】2022.03 冬天下雪了
- Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)
- Oracle 日期函数相关
猜你喜欢
随机推荐
Redux状态管理
浅析v-model语法糖的实现原理与细节知识及如何让你开发的组件支持v-model
文本三剑客之e`grep,seq文本编辑工具
DNS域名解析服务
Oracle 日期函数相关
leetcode 406. Queue Reconstruction by Height 根据身高重建队列(中等)
In-depth analysis of z-index
编辑时过滤当前节点及根据限制的层数过滤数据
《白帽子说Web安全》思维导图
简单谈谈Feign
tidyverse笔记——dplyr包
线程中断方法
shell脚本 -d 是目录文件,那么-e,-f等说明
postgresql源码学习(34)—— 事务日志⑩ - 全页写机制
Basic usage of Koa framework
4-1-7 二叉树及其遍历 家谱处理 (30 分)
浅析瀑布流布局原理及实现方式
【TA-霜狼_may-《百人计划》】美术2.3 硬表面基础
银河麒麟服务器v10 sp2安装oracle19c
搭建zabbix监控及邮件报警(超详细教学)