Skip to content

Commit f62b787

Browse files
committed
记录文件名称使用 sha1,缩减文件名长度
1 parent fec94cd commit f62b787

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/main/java/com/qiniu/storage/persistent/FileRecorder.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.qiniu.storage.persistent;
22

33
import com.qiniu.storage.Recorder;
4-
import com.qiniu.util.UrlSafeBase64;
54

65
import java.io.File;
76
import java.io.FileInputStream;
87
import java.io.FileOutputStream;
98
import java.io.IOException;
9+
import java.security.MessageDigest;
1010
import java.util.Date;
1111

1212
/**
@@ -53,7 +53,7 @@ public FileRecorder(File directory) throws IOException {
5353
*/
5454
@Override
5555
public void set(String key, byte[] data) {
56-
File f = new File(directory, UrlSafeBase64.encodeToString(key));
56+
File f = new File(directory, hash(key));
5757
FileOutputStream fo = null;
5858
try {
5959
fo = new FileOutputStream(f);
@@ -77,7 +77,7 @@ public void set(String key, byte[] data) {
7777
*/
7878
@Override
7979
public byte[] get(String key) {
80-
File f = new File(directory, UrlSafeBase64.encodeToString(key));
80+
File f = new File(directory, hash(key));
8181
if (!f.exists()) {
8282
return null;
8383
}
@@ -119,12 +119,28 @@ private boolean outOfDate(File f) {
119119
*/
120120
@Override
121121
public void del(String key) {
122-
File f = new File(directory, UrlSafeBase64.encodeToString(key));
122+
File f = new File(directory, hash(key));
123123
f.delete();
124124
}
125125

126126
@Override
127127
public String recorderKeyGenerate(String key, File file) {
128-
return key + "_._" + file.getName();
128+
return key + "_._" + file.getAbsolutePath();
129+
}
130+
131+
private static String hash(String base) {
132+
try {
133+
MessageDigest digest = MessageDigest.getInstance("SHA-1");
134+
byte[] hash = digest.digest(base.getBytes());
135+
StringBuffer hexString = new StringBuffer();
136+
137+
for (int i = 0; i < hash.length; i++) {
138+
hexString.append(Integer.toString((hash[i] & 0xff) + 0x100, 16).substring(1));
139+
}
140+
return hexString.toString();
141+
} catch (Exception ex) {
142+
ex.printStackTrace();
143+
}
144+
return null;
129145
}
130146
}

src/test/java/com/qiniu/storage/RecordUploadTest.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
import com.qiniu.http.Response;
99
import com.qiniu.storage.persistent.FileRecorder;
1010
import com.qiniu.util.Etag;
11-
import com.qiniu.util.UrlSafeBase64;
1211
import org.junit.Test;
1312

1413
import java.io.File;
1514
import java.io.IOException;
1615
import java.lang.reflect.InvocationTargetException;
1716
import java.lang.reflect.Method;
17+
import java.security.MessageDigest;
1818
import java.util.Date;
1919
import java.util.Random;
2020

@@ -201,7 +201,7 @@ public void testLastModify() throws IOException {
201201
fr.set(key, data);
202202
byte[] data2 = fr.get(key);
203203

204-
File recoderFile = new File(folder, UrlSafeBase64.encodeToString(key));
204+
File recoderFile = new File(folder, hash(key));
205205

206206
long m1 = recoderFile.lastModified();
207207

@@ -232,6 +232,23 @@ public void testLastModify() throws IOException {
232232
assertTrue(m4 > m1);
233233
}
234234

235+
// copied from FileRecorder
236+
private static String hash(String base) {
237+
try {
238+
MessageDigest digest = MessageDigest.getInstance("SHA-1");
239+
byte[] hash = digest.digest(base.getBytes());
240+
StringBuffer hexString = new StringBuffer();
241+
242+
for (int i = 0; i < hash.length; i++) {
243+
hexString.append(Integer.toString((hash[i] & 0xff) + 0x100, 16).substring(1));
244+
}
245+
return hexString.toString();
246+
} catch (Exception ex) {
247+
ex.printStackTrace();
248+
}
249+
return null;
250+
}
251+
235252
class Up {
236253
private final File file;
237254
private final String key;

0 commit comments

Comments
 (0)