Skip to content

Commit 76a56fe

Browse files
committed
close #141
1 parent 5f71071 commit 76a56fe

File tree

5 files changed

+37
-19
lines changed

5 files changed

+37
-19
lines changed

core/src/main/java/cn/leancloud/AVFile.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,8 @@ public AVFile apply(AVObject avObject) throws Exception {
488488

489489
private Observable<AVFile> saveWithProgressCallback(boolean keepFileName, final ProgressCallback callback) {
490490
JSONObject paramData = generateChangedParam();
491-
final String fileKey = FileUtil.generateFileKey(this.getName(), keepFileName);
492-
paramData.put("key", fileKey);
491+
// final String fileKey = FileUtil.generateFileKey(this.getName(), keepFileName);
492+
// paramData.put("key", fileKey);
493493
paramData.put("__type", "File");
494494
if (StringUtil.isEmpty(getObjectId())) {
495495
if (!StringUtil.isEmpty(getUrl())) {
@@ -505,7 +505,7 @@ public AVFile apply(@NonNull FileUploadToken fileUploadToken) throws Exception {
505505
AVFile.this.internalPutDirectly(KEY_OBJECT_ID, fileUploadToken.getObjectId());
506506
AVFile.this.internalPutDirectly(KEY_BUCKET, fileUploadToken.getBucket());
507507
AVFile.this.internalPutDirectly(KEY_PROVIDER, fileUploadToken.getProvider());
508-
AVFile.this.internalPutDirectly(KEY_FILE_KEY, fileKey);
508+
AVFile.this.internalPutDirectly(KEY_FILE_KEY, fileUploadToken.getKey());
509509

510510
Uploader uploader = new FileUploader(AVFile.this, fileUploadToken, callback);
511511
AVFile.this.internalPutDirectly(KEY_URL, fileUploadToken.getUrl());

core/src/main/java/cn/leancloud/gson/FileUploadTokenAdapter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class FileUploadTokenAdapter extends TypeAdapter<FileUploadToken> {
1717
private static final String FIELD_PROVIDER = "provider";
1818
private static final String FIELD_TOKEN = "token";
1919
private static final String FIELD_URL = "url";
20+
private static final String FIELD_KEY = "key";
2021

2122
public void write(JsonWriter writer, FileUploadToken token) throws IOException {
2223
JsonObject jsonObject = new JsonObject();
@@ -26,6 +27,7 @@ public void write(JsonWriter writer, FileUploadToken token) throws IOException {
2627
jsonObject.addProperty(FIELD_PROVIDER, token.getProvider());
2728
jsonObject.addProperty(FIELD_TOKEN, token.getToken());
2829
jsonObject.addProperty(FIELD_URL, token.getUrl());
30+
jsonObject.addProperty(FIELD_KEY, token.getKey());
2931
TypeAdapters.JSON_ELEMENT.write(writer, jsonObject);
3032
}
3133

@@ -52,6 +54,9 @@ public FileUploadToken read(JsonReader reader) throws IOException {
5254
if (jsonObject.has(FIELD_URL)) {
5355
token.setUrl(jsonObject.get(FIELD_URL).getAsString());
5456
}
57+
if (jsonObject.has(FIELD_KEY)) {
58+
token.setKey(jsonObject.get(FIELD_KEY).getAsString());
59+
}
5560
return token;
5661
}
5762
return null;

core/src/main/java/cn/leancloud/upload/FileUploadToken.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class FileUploadToken {
1515

1616
private String url = null;
1717

18+
private String key = null;
19+
1820
public FileUploadToken() {
1921
}
2022

@@ -66,6 +68,14 @@ public void setUrl(String url) {
6668
this.url = url;
6769
}
6870

71+
public String getKey() {
72+
return key;
73+
}
74+
75+
public void setKey(String key) {
76+
this.key = key;
77+
}
78+
6979
@java.lang.Override
7080
public java.lang.String toString() {
7181
return "FileUploadToken{" +
@@ -75,6 +85,7 @@ public java.lang.String toString() {
7585
", provider='" + provider + '\'' +
7686
", token='" + token + '\'' +
7787
", url='" + url + '\'' +
88+
", key='" + key + '\'' +
7889
'}';
7990
}
8091

@@ -88,11 +99,12 @@ public boolean equals(Object o) {
8899
AVUtils.equals(uploadUrl, that.uploadUrl) &&
89100
AVUtils.equals(provider, that.provider) &&
90101
AVUtils.equals(token, that.token) &&
91-
AVUtils.equals(url, that.url);
102+
AVUtils.equals(url, that.url) &&
103+
AVUtils.equals(key, that.key);
92104
}
93105

94106
@Override
95107
public int hashCode() {
96-
return AVUtils.hash(bucket, objectId, uploadUrl, provider, token, url);
108+
return AVUtils.hash(bucket, objectId, uploadUrl, provider, token, url, key);
97109
}
98110
}

core/src/main/java/cn/leancloud/utils/FileUtil.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ public static void config(MimeTypeDetector mimeTypeDetector) {
1818
detector = mimeTypeDetector;
1919
}
2020

21-
public static String generateFileKey(String name, boolean keepFilename) {
22-
String key = StringUtil.getRandomString(DEFAULT_FILE_KEY_LEN);
23-
int idx = 0;
24-
if (!StringUtil.isEmpty(name)) {
25-
idx = name.lastIndexOf(".");
26-
}
27-
if (keepFilename) {
28-
key += "/" + name;
29-
} else if (idx > 0) {
30-
String postFix = name.substring(idx);
31-
key += postFix;
32-
}
33-
return key;
34-
}
21+
// public static String generateFileKey(String name, boolean keepFilename) {
22+
// String key = StringUtil.getRandomString(DEFAULT_FILE_KEY_LEN);
23+
// int idx = 0;
24+
// if (!StringUtil.isEmpty(name)) {
25+
// idx = name.lastIndexOf(".");
26+
// }
27+
// if (keepFilename) {
28+
// key += "/" + name;
29+
// } else if (idx > 0) {
30+
// String postFix = name.substring(idx);
31+
// key += postFix;
32+
// }
33+
// return key;
34+
// }
3535

3636
public static String getExtensionFromFilename(String filename) {
3737
if (!StringUtil.isEmpty(filename) && Pattern.matches("[a-zA-Z_0-9\\.\\-\\(\\)\\%]+", filename)) {

core/src/test/java/cn/leancloud/upload/FileUploadTokenTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public void testJSONObject() {
2121
token.setToken("tokenV");
2222
token.setUploadUrl("uploadUrlV");
2323
token.setUrl("http://upload.com/file");
24+
token.setKey("dxStIHG3RcqvA6BK5DDJqQCsouDE1IgF/test.jpeg");
2425
String str = JSON.toJSONString(token);
2526
FileUploadToken token2 = JSON.parseObject(str, FileUploadToken.class);
2627
assert token.equals(token2);

0 commit comments

Comments
 (0)