Skip to content

Commit

Permalink
收藏时需要把对应文件拷贝到收藏bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
heavyrain2012 committed Nov 2, 2020
1 parent 8c6b524 commit db0c09c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
5 changes: 4 additions & 1 deletion config/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ media.server.media_type=1
# 使用这个目录作为临时目录,必须配置有效目录。
local.media.temp_storage=/Users/imhao/wildfire_upload_tmp/

## OSS配置,可以是七牛/阿里云OSS/野火私有OSS

## OSS配置,可以是七牛/阿里云OSS/野火私有OSS。
## 注意与IM服务的配置格式不太一样,这里是用"Key=Vaue"的格式,IM服务配置里是"Key Value",拷贝粘贴时要注意修改。

## 配置请参考IM服务
media.server_url=http://up.qbox.me
media.access_key=tU3vdBK5BL5j4N7jI5N5uZgq_HQDo170w5C9Amnn
Expand Down
82 changes: 78 additions & 4 deletions src/main/java/cn/wildfirechat/app/ServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@
import cn.wildfirechat.proto.ProtoConstants;
import cn.wildfirechat.sdk.*;
import cn.wildfirechat.sdk.model.IMResult;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.*;
import com.aliyun.oss.model.CopyObjectResult;
import com.aliyun.oss.model.PutObjectRequest;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
Expand Down Expand Up @@ -53,6 +52,7 @@
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
Expand Down Expand Up @@ -143,6 +143,11 @@ public class ServiceImpl implements Service {
@Value("${media.bucket_moments_domain}")
private String ossMomentsBucketDomain;

@Value("${media.bucket_favorite_name}")
private String ossFavoriteBucket;
@Value("${media.bucket_favorite_domain}")
private String ossFavoriteBucketDomain;

@Value("${local.media.temp_storage}")
private String ossTempPath;

Expand Down Expand Up @@ -925,6 +930,75 @@ public RestResult uploadMedia(int mediaType, MultipartFile file) {
public RestResult putFavoriteItem(FavoriteItem request) {
Subject subject = SecurityUtils.getSubject();
String userId = (String) subject.getSession().getAttribute("userId");

try {
//收藏时需要把对象拷贝到收藏bucket。
URL mediaURL = new URL(request.url);

String bucket = null;
if (mediaURL.getHost().equals(new URL(ossGeneralBucketDomain).getHost())) {
bucket = ossGeneralBucket;
} else if (mediaURL.getHost().equals(new URL(ossImageBucketDomain).getHost())) {
bucket = ossImageBucket;
} else if (mediaURL.getHost().equals(new URL(ossVoiceBucketDomain).getHost())) {
bucket = ossVoiceBucket;
} else if (mediaURL.getHost().equals(new URL(ossVideoBucketDomain).getHost())) {
bucket = ossVideoBucket;
} else if (mediaURL.getHost().equals(new URL(ossFileBucketDomain).getHost())) {
bucket = ossFileBucket;
} else if (mediaURL.getHost().equals(new URL(ossMomentsBucketDomain).getHost())) {
bucket = ossMomentsBucket;
} else if (mediaURL.getHost().equals(new URL(ossStickerBucketDomain).getHost())) {
bucket = ossStickerBucket;
} else if (mediaURL.getHost().equals(new URL(ossFavoriteBucketDomain).getHost())) {
//It's already in fav bucket, no need to copy
//bucket = ossFavoriteBucket;
}

if (bucket != null) {
String path = mediaURL.getPath();
if (ossType == 1) {
Configuration cfg = new Configuration(Region.region0());
String fromKey = path.substring(1);
Auth auth = Auth.create(ossAccessKey, ossSecretKey);

String toBucket = ossFavoriteBucket;
String toKey = fromKey;
if (!toKey.startsWith(userId)) {
toKey = userId + "-" + toKey;
}

BucketManager bucketManager = new BucketManager(auth, cfg);
bucketManager.copy(bucket, fromKey, toBucket, toKey);
request.url = ossFavoriteBucketDomain + "/" + fromKey;
} else if (ossType == 2) {
OSS ossClient = new OSSClient(ossUrl, ossAccessKey, ossSecretKey);
path = path.substring(1);
String objectName = path;
String toKey = path;
if (!toKey.startsWith(userId)) {
toKey = userId + "-" + toKey;
}

ossClient.copyObject(bucket, objectName, ossFavoriteBucket, toKey);
request.url = ossFavoriteBucketDomain + "/" + toKey;
ossClient.shutdown();
} else if (ossType == 3) {
path = path.substring(bucket.length() + 2);
String objectName = path;
String toKey = path;
if (!toKey.startsWith(userId)) {
toKey = userId + "-" + toKey;
}
MinioClient minioClient = new MinioClient(ossUrl, ossAccessKey, ossSecretKey);
minioClient.copyObject(ossFavoriteBucket, toKey, null, null, bucket, objectName, null, null);
request.url = ossFavoriteBucketDomain + "/" + toKey;
}
}
} catch (Exception e) {
e.printStackTrace();
}

request.userId = userId;
request.timestamp = System.currentTimeMillis();
favoriteRepository.save(request);
Expand Down

0 comments on commit db0c09c

Please sign in to comment.