Search in sources :

Example 1 with FileCacheEngine

use of com.zhouzifei.cache.FileCacheEngine in project simpleFS by shengdingbox.

the class FastDfsOssApiClient method checkFile.

@Override
public CheckFileResult checkFile(MetaDataRequest metaDataRequest, HttpServletRequest request) {
    // storageClient.deleteFile(UpLoadConstant.DEFAULT_GROUP, "M00/00/D1/eSqQlFsM_RWASgIyAAQLLONv59s385.jpg");
    String userName = (String) request.getSession().getAttribute("name");
    FileCacheEngine fileCacheEngine = new FileCacheEngine();
    if (StringUtils.isEmpty(userName)) {
        request.getSession().setAttribute("name", "yxqy");
    }
    String fileMd5 = metaDataRequest.getFileMd5();
    if (StringUtils.isEmpty(fileMd5)) {
        throw new ServiceException("fileMd5不能为空");
    }
    CheckFileResult checkFileResult = new CheckFileResult();
    // 模拟从mysql中查询文件表的md5,这里从redis里查询
    // 查询锁占用
    String lockName = UpLoadConstant.currLocks + fileMd5;
    Integer i = fileCacheEngine.get(fileMd5, lockName, Integer.class);
    if (null == i) {
        i = 0;
        fileCacheEngine.add(fileMd5, lockName, "0");
    }
    int lock = i + 1;
    fileCacheEngine.add(fileMd5, lockName, lock);
    String lockOwner = UpLoadConstant.lockOwner + fileMd5;
    String chunkCurrkey = UpLoadConstant.chunkCurr + fileMd5;
    if (lock > 1) {
        checkFileResult.setLock(1);
        // 检查是否为锁的拥有者,如果是放行
        String oWner = fileCacheEngine.get(fileMd5, lockOwner, String.class);
        if (StringUtils.isEmpty(oWner)) {
            throw new ServiceException("无法获取文件锁拥有者");
        } else {
            if (oWner.equals(request.getSession().getAttribute("name"))) {
                String chunkCurr = (String) fileCacheEngine.get(fileMd5, chunkCurrkey);
                if (StringUtils.isEmpty(chunkCurr)) {
                    throw new ServiceException("无法获取当前文件chunkCurr");
                }
                checkFileResult.setChunkCurr(Integer.parseInt(chunkCurr));
                return checkFileResult;
            } else {
                throw new ServiceException("当前文件已有人在上传,您暂无法上传该文件");
            }
        }
    } else {
        // 初始化锁.分块
        fileCacheEngine.add(fileMd5, lockOwner, request.getSession().getAttribute("name"));
        fileCacheEngine.add(fileMd5, chunkCurrkey, "0");
        checkFileResult.setChunkCurr(0);
        return checkFileResult;
    }
}
Also used : ServiceException(com.zhouzifei.tool.common.ServiceException) FileCacheEngine(com.zhouzifei.cache.FileCacheEngine) CheckFileResult(com.zhouzifei.tool.dto.CheckFileResult)

Aggregations

FileCacheEngine (com.zhouzifei.cache.FileCacheEngine)1 ServiceException (com.zhouzifei.tool.common.ServiceException)1 CheckFileResult (com.zhouzifei.tool.dto.CheckFileResult)1