Commit 78c7dd80 by rfkelly0

DAVFS: more accurate size determination when setting Content-Length in uploads

parent 30995c07
...@@ -17,6 +17,7 @@ Requires the dexml module: ...@@ -17,6 +17,7 @@ Requires the dexml module:
# All rights reserved; available under the terms of the MIT License. # All rights reserved; available under the terms of the MIT License.
import os import os
import sys
import httplib import httplib
import socket import socket
from urlparse import urlparse from urlparse import urlparse
...@@ -267,7 +268,6 @@ class DAVFS(FS): ...@@ -267,7 +268,6 @@ class DAVFS(FS):
if resp.status == 409: if resp.status == 409:
raise ParentDirectoryMissingError(path) raise ParentDirectoryMissingError(path)
if resp.status not in (200,201,204): if resp.status not in (200,201,204):
print resp.status
raise_generic_error(resp,"setcontents",path) raise_generic_error(resp,"setcontents",path)
def open(self,path,mode="r"): def open(self,path,mode="r"):
...@@ -302,6 +302,9 @@ class DAVFS(FS): ...@@ -302,6 +302,9 @@ class DAVFS(FS):
contents.size = int(contents.size) contents.size = int(contents.size)
except ValueError: except ValueError:
contents.size = None contents.size = None
if not hasattr(contents,"__exit__"):
contents.__enter__ = lambda *a: contents
contents.__exit__ = lambda *a: contents.close()
return contents return contents
# For everything else, use a RemoteFileBuffer. # For everything else, use a RemoteFileBuffer.
# This will take care of closing the socket when it's done. # This will take care of closing the socket when it's done.
......
...@@ -27,8 +27,22 @@ def get_fileno(file): ...@@ -27,8 +27,22 @@ def get_fileno(file):
else: else:
raise AttributeError raise AttributeError
return file.fileno() return file.fileno()
def get_filesize(file):
"""Get the "size" attribute of a file-like object."""
while not hasattr(file,"size"):
if hasattr(file,"file"):
file = file.file
elif hasattr(file,"_file"):
file = file._file
elif hasattr(file,"_fileobj"):
file = file._fileobj
else:
raise AttributeError
return file.size
def file_chunks(f,chunk_size=1024*64): def file_chunks(f,chunk_size=1024*64):
"""Generator yielding chunks of a file. """Generator yielding chunks of a file.
...@@ -54,7 +68,7 @@ def normalize_req_body(body,chunk_size=1024*64): ...@@ -54,7 +68,7 @@ def normalize_req_body(body,chunk_size=1024*64):
return (len(value),[value]) return (len(value),[value])
elif hasattr(body,"read"): elif hasattr(body,"read"):
try: try:
size = int(body.size) size = int(get_filesize(body))
except (AttributeError,TypeError): except (AttributeError,TypeError):
try: try:
size = os.fstat(get_fileno(body)).st_size size = os.fstat(get_fileno(body)).st_size
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment