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:
# All rights reserved; available under the terms of the MIT License.
import os
import sys
import httplib
import socket
from urlparse import urlparse
......@@ -267,7 +268,6 @@ class DAVFS(FS):
if resp.status == 409:
raise ParentDirectoryMissingError(path)
if resp.status not in (200,201,204):
print resp.status
raise_generic_error(resp,"setcontents",path)
def open(self,path,mode="r"):
......@@ -302,6 +302,9 @@ class DAVFS(FS):
contents.size = int(contents.size)
except ValueError:
contents.size = None
if not hasattr(contents,"__exit__"):
contents.__enter__ = lambda *a: contents
contents.__exit__ = lambda *a: contents.close()
return contents
# For everything else, use a RemoteFileBuffer.
# This will take care of closing the socket when it's done.
......
......@@ -27,8 +27,22 @@ def get_fileno(file):
else:
raise AttributeError
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):
"""Generator yielding chunks of a file.
......@@ -54,7 +68,7 @@ def normalize_req_body(body,chunk_size=1024*64):
return (len(value),[value])
elif hasattr(body,"read"):
try:
size = int(body.size)
size = int(get_filesize(body))
except (AttributeError,TypeError):
try:
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