Skip to content

Commit 489bf86

Browse files
committed
refactor: simplify code according to api updates
and allow users to specify file key with master key
1 parent ebfbbd3 commit 489bf86

2 files changed

Lines changed: 42 additions & 6 deletions

File tree

leancloud/file_.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import re
1010
import io
1111
import hashlib
12-
import uuid
1312
import logging
1413
import threading
1514

@@ -34,6 +33,7 @@ class File(object):
3433

3534
def __init__(self, name="", data=None, mime_type=None):
3635
self._name = name
36+
self.key = None
3737
self.id = None
3838
self._url = None
3939
self._acl = None
@@ -314,21 +314,19 @@ def _update_data(self, server_data):
314314
self._metadata = server_data.get("metaData")
315315

316316
def _get_file_token(self):
317-
key = uuid.uuid4().hex
318-
if self.extension:
319-
key = "{0}.{1}".format(key, self.extension)
320317
data = {
321318
"name": self._name,
322-
"key": key,
323319
"ACL": self._acl,
324320
"mime_type": self.mime_type,
325321
"metaData": self._metadata,
326322
}
323+
if self.key is not None:
324+
data["key"] = self.key
327325
response = client.post("/fileTokens", data)
328326
content = response.json()
329327
self.id = content["objectId"]
330328
self._url = content["url"]
331-
content["key"] = key
329+
self.key = content["key"]
332330
return content
333331

334332
def fetch(self):

tests/test_file.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
import io
99

1010
import six
11+
12+
if six.PY2:
13+
from urlparse import urlparse
14+
if six.PY3:
15+
from urllib.parse import urlparse
16+
1117
import requests
1218
from nose.tools import with_setup # type: ignore
1319
from nose.tools import assert_raises # type: ignore
@@ -24,6 +30,10 @@ def setup_func():
2430
leancloud.init(os.environ["APP_ID"], master_key=os.environ["MASTER_KEY"])
2531

2632

33+
def setup_without_master_key():
34+
leancloud.init(os.environ["APP_ID"], os.environ["APP_KEY"])
35+
36+
2737
def test_basic(): # type: () -> None
2838
def fn(s):
2939
f = File("Blah", s, mime_type="text/plain")
@@ -97,6 +107,34 @@ def test_save(): # type: () -> None
97107
assert not f.url.endswith(".")
98108

99109

110+
@with_setup(setup_func)
111+
def test_save_with_specified_key(): # type: () -> None
112+
f = File("Blah.txt", open("tests/sample_text.txt", "rb"))
113+
user_specified_key = "abc"
114+
f.key = user_specified_key
115+
f.save()
116+
117+
assert f.id
118+
assert f.name == "Blah.txt"
119+
assert f.mime_type == "text/plain"
120+
path = urlparse(f.url).path
121+
if path.startswith("/avos-cloud-"): # old school aws s3 file url
122+
assert path.split("/")[2] == user_specified_key
123+
else:
124+
assert path == "/" + user_specified_key
125+
126+
127+
@with_setup(setup_without_master_key)
128+
def test_save_with_specified_key_but_without_master_key(): # type: () -> None
129+
f = File("Blah.txt", open("tests/sample_text.txt", "rb"))
130+
f.key = "abc"
131+
try:
132+
f.save()
133+
except LeanCloudError as e:
134+
if e.code == 1 and e.message.startswith("Unsupported file key"):
135+
pass
136+
137+
100138
@with_setup(setup_func)
101139
def test_query(): # type: () -> None
102140
files = leancloud.Query("File").find()

0 commit comments

Comments
 (0)