当前位置:网站首页>文件 - 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>
边栏推荐
- Markdown中的数学符号
- FTP服务与配置
- 试题 历届真题 错误票据【第四届】【省赛】【B组】
- Automatic translation software - batch batch automatic translation software recommendation
- Chapter 16: Constructing the Magic Square for Prime Numbers of Order n(5,7)
- Analysis of pseudo-classes and pseudo-elements
- 防抖和节流
- 浅析v-model语法糖的实现原理与细节知识及如何让你开发的组件支持v-model
- Zero-Shot Learning & Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning
- 第十六章:构建n(5,7)阶素数幻方
猜你喜欢
随机推荐
codec2 BlockPool:不可读库
Obtaining server and client information
mysql的下载及安装使用
nohup原理
《白帽子说Web安全》思维导图
Redux state management
拓扑排序的两种方法 --- dfs+Kahn
(border-box) The difference between box model w3c and IE
服务器和客户端信息的获取
Run the NPM will pop up to ask "how are you going to open this file?"
MySQL的触发器
线程唤醒机制
SSH远程管理
银河麒麟服务器v10 sp1安装.net6
一文读懂 MongoDB 和 MySQL 的差异
银河麒麟服务器v10 sp2安装oracle19c
【云原生】-Docker容器迁移Oracle到MySQL
Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)
Database Principles Homework 2 — JMU
快速傅里叶变换(FFT)