Skip to content

Commit

Permalink
Merge pull request langhsu#17 from saxingz/patch-1
Browse files Browse the repository at this point in the history
Picture cleaning and recycling automatically
  • Loading branch information
langhsu authored Apr 5, 2019
2 parents 21bfaa2 + 09ad13e commit be57d84
Show file tree
Hide file tree
Showing 16 changed files with 586 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ rebel.xml

*.db

!/src/main/java/com/mtons/mblog/base/storage/
5 changes: 5 additions & 0 deletions src/main/java/com/mtons/mblog/base/lang/Consts.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public interface Consts {
*/
String avatarPath = "/storage/avatars";

/**
* 图片标记
*/
public static final String PIC_MARK = "/809c82722ec1492288a4bb4c50955b50/";

/**
* 默认头像
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

import com.mtons.mblog.base.lang.MtonsException;
import com.mtons.mblog.base.storage.Storage;
import com.mtons.mblog.base.utils.FileKit;
import com.mtons.mblog.base.utils.FilePathUtils;
import com.mtons.mblog.base.utils.ImageUtils;
import com.mtons.mblog.base.utils.*;
import com.mtons.mblog.config.SiteOptions;
import com.mtons.mblog.modules.entity.Pic;
import com.mtons.mblog.modules.repository.PicRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -27,6 +27,8 @@
public abstract class AbstractStorage implements Storage {
@Autowired
protected SiteOptions options;
@Autowired
protected PicRepository picRepository;

protected void validateFile(MultipartFile file) {
if (file == null || file.isEmpty()) {
Expand Down Expand Up @@ -59,7 +61,22 @@ public String storeScale(MultipartFile file, String basePath, int width, int hei
}

public String writeToStore(byte[] bytes, String src, String originalFilename) throws Exception {
String path = FilePathUtils.wholePathName(src, originalFilename);
return writeToStore(bytes, path);
String md5 = MD5.md5File(bytes);
long id = IdUtils.getId();
Pic pic = picRepository.findByMd5(md5);
if (pic != null){
return pic.getPath();
}
String path = FilePathUtils.wholePathName(src, originalFilename, id);
writeToStore(bytes, writeToStore(bytes, path));
// 图片入库
pic = new Pic();
pic.setId(id);
pic.setMd5(md5);
pic.setPath(path);
pic.setAmount(0);
picRepository.save(pic);
return path;
}

}
21 changes: 10 additions & 11 deletions src/main/java/com/mtons/mblog/base/utils/FilePathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
*/
package com.mtons.mblog.base.utils;

import org.apache.commons.lang3.time.DateFormatUtils;
import com.mtons.mblog.base.lang.Consts;
import org.apache.commons.text.RandomStringGenerator;

import java.util.Date;

/**
* @author langhsu
*
Expand All @@ -16,7 +14,7 @@ public class FilePathUtils {
private static final int[] AVATAR_GRIDS = new int[] {3,3,3};
private static final int AVATAR_LENGTH = 9;

private static final String YMDHMS = "/yyyy/MMdd/ddHHmmss";
private static final String Y = "/yyyy/";

private static RandomStringGenerator randomString = new RandomStringGenerator.Builder().withinRange('a', 'z').build();

Expand All @@ -42,22 +40,23 @@ public static String getAvatar(long key) {
* @param originalFilename 原始文件名
* @return 10位长度文件名+文件后缀
*/
public static String wholePathName(String originalFilename) {
StringBuilder builder = new StringBuilder(28);
builder.append(DateFormatUtils.format(new Date(), YMDHMS));
builder.append(randomString.generate(4));
public static String wholePathName(String originalFilename, Long id) {
StringBuilder builder = new StringBuilder(52);
builder.append(Consts.PIC_MARK);
builder.append(id);
builder.append(FileKit.getSuffix(originalFilename));
return builder.toString();
}

public static String wholePathName(String basePath, String ext) {
return basePath + wholePathName(ext);
public static String wholePathName(String basePath, String ext, Long id) {
return basePath + wholePathName(ext, id);
}

public static void main(String[] args) {
String base = FilePathUtils.getAvatar(50);
System.out.println(String.format("/%s_%d.jpg", base, 100));
System.out.println(FilePathUtils.wholePathName("a.jpg"));
System.out.println(FilePathUtils.wholePathName("a.jpg", IdUtils.getId()));
System.out.println(IdUtils.getId());
}

}
30 changes: 30 additions & 0 deletions src/main/java/com/mtons/mblog/base/utils/IdUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.mtons.mblog.base.utils;

import java.util.concurrent.atomic.AtomicInteger;

/**
* id generate
*
* @author saxing 2019/4/5 9:15
*/
public class IdUtils {

private final static AtomicInteger resource = ResourceLock.getAtomicInteger("ID_GENERATE");

/**
* 生成id
*
* @return
*/
public static long getId(){
synchronized (resource){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return System.currentTimeMillis();
}
}

}
38 changes: 28 additions & 10 deletions src/main/java/com/mtons/mblog/base/utils/MD5.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ public class MD5 {

/**
* 对字符串进行Md5加密
*
*
* @param input 原文
* @return md5后的密文
*/
public static String md5(String input) {
byte[] code = null;
try {
code = MessageDigest.getInstance("md5").digest(input.getBytes());
} catch (NoSuchAlgorithmException e) {
code = input.getBytes();
}
BigInteger bi = new BigInteger(code);
return bi.abs().toString(32).toUpperCase();
try {
code = MessageDigest.getInstance("md5").digest(input.getBytes());
} catch (NoSuchAlgorithmException e) {
code = input.getBytes();
}
BigInteger bi = new BigInteger(code);
return bi.abs().toString(32).toUpperCase();
}

/**
* 对字符串进行Md5加密
*
*
* @param input 原文
* @param salt 随机数
* @return string
Expand All @@ -51,4 +51,22 @@ public static String md5(String input, String salt) {
return md5(salt + md5(input));
}

/**
* 文件md5计算
*
* @param bytes
* @return
* @throws NoSuchAlgorithmException
*/
public static String md5File(byte[] bytes) {
byte[] code = new byte[0];
try {
code = MessageDigest.getInstance("md5").digest(bytes);
} catch (NoSuchAlgorithmException e) {
return "";
}
BigInteger bi = new BigInteger(code);
return bi.abs().toString(32).toUpperCase();
}

}
40 changes: 40 additions & 0 deletions src/main/java/com/mtons/mblog/base/utils/ResourceLock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.mtons.mblog.base.utils;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
* resource lock
*
* @author saxing 2019/4/3 16:20
*/
public class ResourceLock {

private static final ConcurrentHashMap<String, AtomicInteger> lockMap = new ConcurrentHashMap<String, AtomicInteger>();

public static AtomicInteger getAtomicInteger(String key) {
if (lockMap.get(key) == null) {
lockMap.putIfAbsent(key, new AtomicInteger(0));
}
int count = lockMap.get(key).incrementAndGet();
return lockMap.get(key);
}

public static void giveUpAtomicInteger(String key) {
if (lockMap.get(key) != null) {
int source = lockMap.get(key).decrementAndGet();
if (source <= 0) {
lockMap.remove(key);
}
}
}

public static String getPostKey(Long postId){
return "POST_OPERATE_{postId}".replace("{postId}", String.valueOf(postId));
}

public static String getPicKey(Long picId){
return "PIC_OPERATE_{pic}".replace("{pic}", String.valueOf(picId));
}

}
84 changes: 84 additions & 0 deletions src/main/java/com/mtons/mblog/modules/entity/Pic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.mtons.mblog.modules.entity;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
* 图片
*
* @author saxing 2019/4/3 21:24
*/
@Entity
@Table(name = "mto_pic",
uniqueConstraints = {@UniqueConstraint(name = "md5key", columnNames = {"md5"})}
)
public class Pic implements Serializable {
private static final long serialVersionUID = -2263990565349962964L;

@Id
private long id;

@Column(name = "md5", columnDefinition = "varchar(50) NOT NULL DEFAULT ''")
private String md5;

@Column(name = "path", columnDefinition = "varchar(255) NOT NULL DEFAULT ''")
private String path;

@Column(name = "amount", columnDefinition = "bigint(20) NOT NULL DEFAULT '0'")
private long amount;

@Column(name = "create_time")
private LocalDateTime createTime;

@Column(name = "update_time")
private LocalDateTime updateTime;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getMd5() {
return md5;
}

public void setMd5(String md5) {
this.md5 = md5;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public long getAmount() {
return amount;
}

public void setAmount(long amount) {
this.amount = amount;
}

public LocalDateTime getCreateTime() {
return createTime;
}

public void setCreateTime(LocalDateTime createTime) {
this.createTime = createTime;
}

public LocalDateTime getUpdateTime() {
return updateTime;
}

public void setUpdateTime(LocalDateTime updateTime) {
this.updateTime = updateTime;
}
}
Loading

0 comments on commit be57d84

Please sign in to comment.