FileSupportService.java 10.9 KB
Newer Older
苗卫卫 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
package com.starcharge.component.datastore;

import com.ihidea.core.CoreConstants;
import com.ihidea.core.base.CoreService;
import com.ihidea.core.support.exception.ServiceException;
import com.ihidea.core.support.orm.mybatis3.util.IbatisServiceUtils;
import com.ihidea.core.support.session.SessionContext;
import com.ihidea.core.util.DateUtilsEx;
import com.ihidea.core.util.FileUtilsEx;
import com.ihidea.core.util.ImageUtilsEx;
import com.ihidea.core.util.StringUtilsEx;
import com.starcharge.component.datastore.dao.CptDataInfoMapper;
import com.starcharge.component.datastore.dao.model.CptDataInfo;
import com.starcharge.component.datastore.dao.model.CptDataInfoCriteria;
import com.starcharge.component.datastore.fileio.FileIoEntity;
import com.starcharge.component.datastore.fileio.FileIoFactory;
import com.starcharge.component.datastore.fileio.IFileInputStream;
import com.starcharge.component.datastore.fileio.IFileIo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service("fileStoreInfoService")
public class FileSupportService extends CoreService {

	@Autowired
	private CptDataInfoMapper dataInfoDao;

	@Autowired
	private DataStoreService dataStoreService;

	/**
	 * @param fileName 文件名
	 * @param fileContent 文件二进制内容
	 * @param storeName cpt_datastore.name
	 * @param fileImgSize 图片压缩尺寸,100|200,100|0,0|300 或 格式 100,200,100,0,0,300 或 格式 100x200,100x0,0x300
	 * @return
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public String add(String fileId, String fileName, byte[] fileContent, String storeName, String fileImgSize, String prefix) {

		CptDataInfo dataInfo = new CptDataInfo();

		// 文件信息
		{
			// 文件编号加入后缀名
			if (StringUtils.isBlank(fileId)) {
				if ("false".equals(CoreConstants.getProperty("filestore.enable.suffix"))) {
					dataInfo.setId(StringUtilsEx.getUUID());
				} else {
					dataInfo.setId(StringUtilsEx.getUUID() + "." + FileUtilsEx.getSuffix(fileName));
				}
			}

			// 有可能是非session
			dataInfo.setCreateAccount(SessionContext.getSessionInfo() != null ? SessionContext.getSessionInfo().getUserId() : null);

			dataInfo.setCreateTime(DateUtilsEx.getSysDate());

			dataInfo.setFileName(fileName);

			dataInfo.setFileSize(fileContent.length);

			dataInfo.setStatus(0);

			dataInfo.setStoreName(storeName);
			
			dataInfo.setPrefix(prefix);
		}

		// 如果设置图片尺寸,并且是图片类型的文件则进行压缩
		if (StringUtils.isNotBlank(fileImgSize) && FileUtilsEx.isImage(fileName)) {

			fileImgSize = fileImgSize.replace(",", "|").replace("x", "|");

			String[] sizeArray = fileImgSize.split("\\|");

			try {
				fileContent = ImageUtilsEx.resizeImage(fileContent, Integer.valueOf(sizeArray[0]), Integer.valueOf(sizeArray[1]),
						FileUtilsEx.getSuffix(fileName));

				// 重新尺寸
				dataInfo.setFileSize(fileContent.length);
			} catch (Exception e) {
				logger.error("图片压缩出错:" + e.getMessage(), e);
			}
		}

		// 文件实际保存
		{
			IFileIo fileIo = FileIoFactory.getInstance(storeName);

			FileIoEntity entity = new FileIoEntity();
			entity.setDataInfo(dataInfo);
			entity.setContent(fileContent);

			fileIo.save(entity);
		}

		// 图片的话,设置具体尺寸
		setDescription(dataInfo, fileContent);

		// 文件信息保存
		dataInfoDao.insert(dataInfo);

		return dataInfo.getId();
	}
	
	public String add(String fileId, String fileName, byte[] fileContent, String storeName, String fileImgSize) {
		return add(fileId, fileName, fileContent, storeName, fileImgSize, null);
	}

	public String add(String fileName, byte[] fileContent, String storeName, String fileImgSize) {
		return add(null, fileName, fileContent, storeName, fileImgSize);
	}

	public String add(String fileName, byte[] fileContent, String storeName) {
		return add(null, fileName, fileContent, storeName, null);
	}

	/**
	 * 设置文件描述
	 * @param dataInfo
	 * @param fileContent
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	protected void setDescription(CptDataInfo dataInfo, byte[] fileContent) {

		if (!FileUtilsEx.isImage(dataInfo.getFileName())){
			return;
		}
		try {
			BufferedImage image = ImageIO.read(new ByteArrayInputStream(fileContent));
			image.getHeight();
			dataInfo.setDescription(String.valueOf(image.getWidth()) + "|" + String.valueOf(image.getHeight()));
		} catch (IOException e) {
			this.logger.error(e.getMessage(), e);
		}
	}

	/**
	 * 增加附件,不提交的话查询不到附件
	 * @param fileName 文件名
	 * @param fileContent 文件二进制内容
	 * @return
	 */
	public String add(String fileName, byte[] fileContent) {
		return add(fileName, fileContent, "default");
	}

	/**
	 * 提交文件,正式存储入库
	 * @param id 文件编号
	 * @param 文件描述
	 * @throws Exception
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void submit(String id, String desc) {

		if (StringUtils.isBlank(desc)) {
			throw new ServiceException("提交入库的文件,必须要有描述!");
		}

		CptDataInfo dataInfo = dataInfoDao.selectByPrimaryKey(id);
		dataInfo.setStatus(1);
		dataInfo.setDescription(desc);

		IbatisServiceUtils.updateByPk(dataInfo, dataInfoDao);

		// 备份文件保存
		{
			IFileIo fileIo = FileIoFactory.getInstance(dataInfo.getStoreName());
			FileIoEntity entity = get(id);

			if (!StringUtils.isEmpty(dataStoreService.getInfoByName(entity.getDataInfo().getStoreName()).getBakPath())){
				fileIo.saveBak(entity);
			}
		}
	}

	/**
	 * 得到文件信息
	 * @param id 文件id
	 * @return
	 */
	public FileIoEntity get(String id) {

		FileIoEntity entity = new FileIoEntity();

		CptDataInfo dataInfo = dataInfoDao.selectByPrimaryKey(id);

		if (dataInfo != null) {
			IFileIo fileIo = FileIoFactory.getInstance(dataInfo.getStoreName());
			entity.setContent(fileIo.get(id));
			entity.setDataInfo(dataInfo);
		}

		return entity;
	}

	public FileIoEntity getInfo(String id) {

		FileIoEntity entity = new FileIoEntity();

		CptDataInfo dataInfo = dataInfoDao.selectByPrimaryKey(id);

		if (dataInfo != null) {
			entity.setDataInfo(dataInfo);
		}

		return entity;
	}

	/**
	 * 不得到文件内容,得到文件的信息
	 * @param id
	 * @return
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public FileIoEntity execute(String id, IFileInputStream fileInputStreamImpl) throws Exception {

		CptDataInfo dataInfo = dataInfoDao.selectByPrimaryKey(id);

		// TODO 兼容没有后缀名的文件
		if (dataInfo == null && StringUtils.isNotBlank(id) && id.indexOf(".") > -1) {
			dataInfo = dataInfoDao.selectByPrimaryKey(id.substring(0, id.indexOf(".")));
		}

		FileIoEntity entity = new FileIoEntity(dataInfo);

		if (dataInfo != null && StringUtils.isNotBlank(dataInfo.getStoreName()) && StringUtils.isNotBlank(dataInfo.getId())) {

			IFileIo fileIo = FileIoFactory.getInstance(dataInfo.getStoreName());
			fileIo.execute(entity, fileInputStreamImpl);
		} else {
			fileInputStreamImpl.execute(null, null);
		}

		return entity;
	}

	/**
	 * 得到文件描述信息
	 * @param id 文件id
	 * @return
	 */
	public List<FileIoEntity> getInfoByIds(String ids) {

		List<FileIoEntity> resultList = new ArrayList<FileIoEntity>();

		if (StringUtils.isNotBlank(ids)) {
			String[] idArray = ids.split(",");

			List<CptDataInfo> list=findByIds(Arrays.asList(idArray));
			for (CptDataInfo info : list) {
				FileIoEntity entity = new FileIoEntity();
				entity.setDataInfo(info);
				resultList.add(entity);
			}
		}

		return resultList;
	}
	
	public List<CptDataInfo> findByIds(List<String> idList){
		CptDataInfoCriteria example = new CptDataInfoCriteria();
		CptDataInfoCriteria.Criteria c=example.createCriteria();
		c.andIdIn(idList);
		return dataInfoDao.selectByExample(example);
	}

	/**
	 * 通过文件名找到文件
	 * @param fileName 文件名
	 * @param storeName 存储名
	 * @return
	 */
	public List<FileIoEntity> getByName(String fileName, String storeName) {

		CptDataInfo dataInfo = new CptDataInfo();
		dataInfo.setFileName(fileName);
		dataInfo.setStoreName(storeName);

		List<CptDataInfo> dataInfoList = IbatisServiceUtils.find(dataInfo, dataInfoDao);

		IFileIo fileIo = FileIoFactory.getInstance(dataInfo.getStoreName());

		List<FileIoEntity> result = new ArrayList<FileIoEntity>();

		for (CptDataInfo dataInfoT : dataInfoList) {

			FileIoEntity entity = new FileIoEntity();

			entity.setContent(fileIo.get(dataInfoT.getId()));
			entity.setDataInfo(dataInfoT);

			result.add(entity);
		}

		return result;
	}

	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void remove(String id) {

		if (StringUtils.isNotBlank(id)) {
			CptDataInfo dataInfo = dataInfoDao.selectByPrimaryKey(id);

			IFileIo fileIo = FileIoFactory.getInstance(dataInfo.getStoreName());

			FileIoEntity entity = new FileIoEntity();
			entity.setDataInfo(dataInfo);

			if (fileIo.remove(entity)) {
				dataInfoDao.deleteByPrimaryKey(id);
			} else {
				throw new ServiceException("文件:" + id + "删除失败");
			}
		}
	}

	/**
	 * 根据name删除历史数据
	 * @param fileName
	 * @return
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void removeByName(String fileName, String storeName) {

		List<FileIoEntity> res = getByName(fileName, storeName);

		for (int i = 0; i < res.size(); i++) {
			CptDataInfo dataInfo = res.get(i).getDataInfo();

			remove(dataInfo.getId());
		}
	}

	/**
	 * 更新文件内容
	 * @param id
	 * @param content
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void updateContent(String id, byte[] content) {

		CptDataInfo dataInfo = dataInfoDao.selectByPrimaryKey(id);

		dataInfo.setFileSize(content.length);

		// 修改文件长度
		IbatisServiceUtils.updateByPk(dataInfo, dataInfoDao);

		IFileIo fileIo = FileIoFactory.getInstance(dataInfo.getStoreName());

		// 修改文件内容
		fileIo.updateContent(id, content);
	}

	/**
	 * 清理临时数据
	 */
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void clearTemp(String storeName) {

		IFileIo fileIo = FileIoFactory.getInstance(storeName);

		// 找出所有状态为0的数据
		CptDataInfo searchEntity = new CptDataInfo();
		searchEntity.setStoreName(storeName);
		searchEntity.setStatus(0);

		for (CptDataInfo tempData : IbatisServiceUtils.find(searchEntity, dataInfoDao)) {

			// 移除数据内容
			FileIoEntity fileIoEntity = new FileIoEntity();
			fileIoEntity.setDataInfo(tempData);

			// 如果正常删除,则修改状态
			if (fileIo.remove(fileIoEntity)) {

				tempData.setStatus(1);
				dataInfoDao.updateByPrimaryKey(tempData);
			}

		}
	}
}