/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
 * not liable for any claims arising from secondary or illegal development.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package org.springblade.file.service.impl;

import org.springblade.core.log.exception.ServiceException;
import org.springblade.file.pojo.vo.PartEtagVO;
import org.springblade.file.service.IFileService;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.List;

/**
 * 文件服务基础实现
 *
 * @author Chill
 */
public abstract class AbstractFileService implements IFileService {

	@Override
	public String initiateMultipartUpload(String objectKey) {
		throw new ServiceException("当前文件存储不支持分段上传");
	}

	@Override
	public PartEtagVO uploadPart(String objectKey, String uploadId, File file, int partNumber, Long offset, Long partSize) {
		throw new ServiceException("当前文件存储不支持分段上传");
	}

	@Override
	public void completeMultipartUpload(String objectKey, String uploadId, List<PartEtagVO> partEtagVOS) {
		throw new ServiceException("当前文件存储不支持分段上传");
	}

	@Override
	public void cancelMultipartUpload(String objectKey, String uploadId) {
		throw new ServiceException("当前文件存储不支持分段上传");
	}

	@Override
	public String getUploadPartUrl(String objectKey, String uploadId, Integer partNumber, Long expires) {
		throw new ServiceException("当前文件存储不支持分段上传");
	}

	@Override
	public String getFileUrl(String objectKey, Long expires) {
		return this.getFileUrl(objectKey, null, expires);
	}

	@Override
	public List<String> getFileUrls(List<String> objectKeys, Long expires) {
		return objectKeys.stream()
			.map(objectKey -> this.getFileUrl(objectKey, expires))
			.toList();
	}

	@Override
	public String getFileMd5(File file) {
		try (FileInputStream inputStream = new FileInputStream(file)) {
			MessageDigest messageDigest = MessageDigest.getInstance("MD5");
			byte[] buffer = new byte[8192];
			int length;
			while ((length = inputStream.read(buffer)) != -1) {
				messageDigest.update(buffer, 0, length);
			}
			return Base64.getEncoder().encodeToString(messageDigest.digest());
		} catch (NoSuchAlgorithmException | IOException exception) {
			throw new ServiceException("获取文件md5异常");
		}
	}
}
