Skip to content

Commit d82fc29

Browse files
authored
Merge pull request #242 from sxci/hash_record_file_name
记录文件名称使用 sha1,缩减文件名长度
2 parents 45e5df8 + 62456f4 commit d82fc29

File tree

6 files changed

+60
-11
lines changed

6 files changed

+60
-11
lines changed

src/main/java/com/qiniu/storage/ResumeUploader.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ public final class ResumeUploader {
6868
}
6969

7070
public Response upload() throws QiniuException {
71+
try {
72+
return upload0();
73+
} finally {
74+
close();
75+
}
76+
}
77+
78+
private Response upload0() throws QiniuException {
7179
if (host == null) {
7280
this.host = configuration.upHost(upToken);
7381
}

src/main/java/com/qiniu/storage/StreamUploader.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public final class StreamUploader {
4646
}
4747

4848
public Response upload() throws QiniuException {
49+
try {
50+
return upload0();
51+
} finally {
52+
close();
53+
}
54+
}
55+
56+
private Response upload0() throws QiniuException {
4957
if (host == null) {
5058
this.host = configuration.upHost(upToken);
5159
}

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;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void testXVar() throws IOException {
4040
StringMap m = res.jsonToMap();
4141
assertEquals("foo_val", m.get("foo"));
4242
} catch (QiniuException e) {
43-
assertEquals("", e.response.bodyString());
43+
assertEquals("", e.response == null ? "e.response is null" : e.response.bodyString());
4444
fail();
4545
} finally {
4646
TempFile.remove(f);
@@ -69,7 +69,7 @@ private void template(int size, boolean https) throws IOException {
6969
assertEquals(String.valueOf(f.length()), ret.fsize);
7070
assertEquals(etag, ret.hash);
7171
} catch (QiniuException e) {
72-
assertEquals("", e.response.bodyString());
72+
assertEquals("", e.response == null ? "e.response is null" : e.response.bodyString());
7373
fail();
7474
}
7575
TempFile.remove(f);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void testXVar() throws IOException {
4444
StringMap m = res.jsonToMap();
4545
assertEquals("foo_val", m.get("foo"));
4646
} catch (QiniuException e) {
47-
assertEquals("", e.response.bodyString());
47+
assertEquals("", e.response == null ? "e.response is null" : e.response.bodyString());
4848
fail();
4949
} finally {
5050
TempFile.remove(f);
@@ -74,7 +74,7 @@ private void template(int size, boolean https) throws IOException {
7474
assertEquals(mime, ret.mimeType);
7575
assertEquals(etag, ret.hash);
7676
} catch (QiniuException e) {
77-
assertEquals("", e.response.bodyString());
77+
assertEquals("", e.response == null ? "e.response is null" : e.response.bodyString());
7878
fail();
7979
}
8080
TempFile.remove(f);

0 commit comments

Comments
 (0)