11package com .qiniu .storage .persistent ;
22
33import com .qiniu .storage .Recorder ;
4- import com .qiniu .util .UrlSafeBase64 ;
54
65import java .io .File ;
76import java .io .FileInputStream ;
87import java .io .FileOutputStream ;
98import java .io .IOException ;
9+ import java .security .MessageDigest ;
1010import 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}
0 commit comments