Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.github.tobato.fastdfs.domain.fdfs.StorageNode;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.fdfs.ThumbImageConfig;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadCallback;
import com.github.tobato.fastdfs.domain.proto.storage.StorageSetMetadataCommand;
import com.github.tobato.fastdfs.domain.proto.storage.StorageUploadFileCommand;
import com.github.tobato.fastdfs.domain.proto.storage.StorageUploadSlaveFileCommand;
Expand Down Expand Up @@ -92,6 +93,19 @@ public StorePath uploadImageAndCrtThumbImage(InputStream inputStream,
return uploadImage(fastImageFile);
}

@Override
public StorePath createThumbImage(final StorePath storePath, final ThumbImage thumbImage) {
final StorageNode client = getStorageNode(storePath.getGroup());
final String fileExtName = storePath.getPath().substring(storePath.getPath().lastIndexOf(".") + 1);
return downloadFile(storePath.getGroup(), storePath.getPath(),
new DownloadCallback<StorePath>() {
@Override
public StorePath recv(InputStream inputStream) throws IOException {
return uploadThumbImage(client, inputStream, storePath.getPath(), thumbImage, fileExtName);
}
});
}

/**
* 上传文件
* <pre>
Expand Down Expand Up @@ -149,7 +163,8 @@ public StorePath uploadImage(FastImageFile fastImageFile) {
//如果设置了需要上传缩略图
if (null != fastImageFile.getThumbImage()) {
// 上传缩略图
uploadThumbImage(client, new ByteArrayInputStream(bytes), path.getPath(), fastImageFile);
uploadThumbImage(client, new ByteArrayInputStream(bytes), path.getPath(),
fastImageFile.getThumbImage(), fileExtName);
}
bytes = null;
return path;
Expand Down Expand Up @@ -236,12 +251,13 @@ private StorePath uploadFileAndMetaData(StorageNode client, InputStream inputStr
* @param client
* @param inputStream
* @param masterFilename
* @param fastImageFile
* @param thumbImage
*/
private void uploadThumbImage(StorageNode client, InputStream inputStream,
String masterFilename, FastImageFile fastImageFile) {
private StorePath uploadThumbImage(StorageNode client, InputStream inputStream,
String masterFilename, ThumbImage thumbImage,
String fileExtName) {
ByteArrayInputStream thumbImageStream = null;
ThumbImage thumbImage = fastImageFile.getThumbImage();
StorePath storePath = null;
try {
//生成缩略图片
thumbImageStream = generateThumbImageStream(inputStream, thumbImage);
Expand All @@ -253,15 +269,16 @@ private void uploadThumbImage(StorageNode client, InputStream inputStream,
LOGGER.debug("获取到缩略图前缀{}", prefixName);
}
StorageUploadSlaveFileCommand command = new StorageUploadSlaveFileCommand(thumbImageStream, fileSize,
masterFilename, prefixName, fastImageFile.getFileExtName());
fdfsConnectionManager.executeFdfsCmd(client.getInetSocketAddress(), command);
masterFilename, prefixName, fileExtName);
storePath = fdfsConnectionManager.executeFdfsCmd(client.getInetSocketAddress(), command);

} catch (IOException e) {
LOGGER.error("upload ThumbImage error", e);
throw new FdfsUploadImageException("upload ThumbImage error", e.getCause());
} finally {
IOUtils.closeQuietly(thumbImageStream);
}
return storePath;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.upload.FastFile;
import com.github.tobato.fastdfs.domain.upload.FastImageFile;
import com.github.tobato.fastdfs.domain.upload.ThumbImage;

import java.io.InputStream;
import java.util.Set;
Expand Down Expand Up @@ -49,6 +50,14 @@ public interface FastFileStorageClient extends GenerateStorageClient {
StorePath uploadImageAndCrtThumbImage(InputStream inputStream, long fileSize, String fileExtName,
Set<MetaData> metaDataSet);

/**
* 根据已有源图生成缩略图
* @param storePath 源图片储存地址
* @param thumbImage 缩略图配置
* @return 缩略图储存地址
*/
StorePath createThumbImage(StorePath storePath, ThumbImage thumbImage);

/**
* 上传图片
* <pre>
Expand Down