Skip to content

Commit

Permalink
Made FileRef.upload() raise FileNotFound if file does not exist on th…
Browse files Browse the repository at this point in the history
…e file server
  • Loading branch information
Erik Allik committed Aug 28, 2013
1 parent 4c8b24a commit 234537e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 8 additions & 1 deletion spinoff/contrib/filetransfer/fileref.py
Expand Up @@ -100,7 +100,10 @@ def fetch(self, dst_path=None, on_progress=None):
return ret

def upload(self, url):
return self.server.ask(('upload', self.file_id, url))
success, response = self.server.ask(('upload', self.file_id, url))
if not success:
raise FileNotFound
return response

def _transfer(self, fh, on_progress):
request = get_context().spawn(Request.using(server=self.server, file_id=self.file_id, size=self.size, abstract_path=self.abstract_path))
Expand Down Expand Up @@ -132,6 +135,10 @@ class TransferFailed(Exception):
pass


class FileNotFound(Exception):
pass


def move_or_copy(src, dst):
try:
os.rename(src, dst)
Expand Down
5 changes: 3 additions & 2 deletions spinoff/contrib/filetransfer/server.py
Expand Up @@ -32,6 +32,7 @@ def receive(self, msg):
elif msg == ('request', ANY) or msg == ('request-local', ANY):
request, file_id = msg
if file_id not in self.published:
# TODO: replace with a reply to the requestor, just like in the upload() case
err("attempt to get a file with ID %r which has not been published or is not available anymore" % (file_id,))
else:
file_path, time_added = self.published[file_id]
Expand All @@ -55,12 +56,12 @@ def receive(self, msg):
elif ('upload', ANY, ANY) == msg:
_, file_id, url = msg
if file_id not in self.published:
self.reply((False, "Attempted to upload a file with ID %r which has not been published or is not available anymore" % (file_id,)))
self.reply((False, None))
else:
self._touch_file(file_id)
file_path, _ = self.published[file_id]
r = requests.post(url, files={'file': open(file_path, 'rb')})
self.reply((True, r.text))
self.reply((True, (r.status_code, dict(r.headers), r.text)))

def _touch_file(self, file_id):
file_path, time_added = self.published[file_id]
Expand Down

0 comments on commit 234537e

Please sign in to comment.