mirror of https://github.com/django/django.git
parent
c94bff2b26
commit
96fc3908ad
|
@ -36,19 +36,26 @@ class File(FileProxyMixin):
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return self.size
|
return self.size
|
||||||
|
|
||||||
def _get_size(self):
|
def _get_size_from_underlying_file(self):
|
||||||
if not hasattr(self, '_size'):
|
|
||||||
if hasattr(self.file, 'size'):
|
if hasattr(self.file, 'size'):
|
||||||
self._size = self.file.size
|
return self.file.size
|
||||||
elif hasattr(self.file, 'name') and self.file.name is not None and os.path.exists(self.file.name):
|
if hasattr(self.file, 'name'):
|
||||||
self._size = os.path.getsize(self.file.name)
|
try:
|
||||||
elif hasattr(self.file, 'tell') and hasattr(self.file, 'seek'):
|
return os.path.getsize(self.file.name)
|
||||||
|
except (OSError, TypeError):
|
||||||
|
pass
|
||||||
|
if hasattr(self.file, 'tell') and hasattr(self.file, 'seek'):
|
||||||
pos = self.file.tell()
|
pos = self.file.tell()
|
||||||
self.file.seek(0, os.SEEK_END)
|
self.file.seek(0, os.SEEK_END)
|
||||||
self._size = self.file.tell()
|
size = self.file.tell()
|
||||||
self.file.seek(pos)
|
self.file.seek(pos)
|
||||||
else:
|
return size
|
||||||
raise AttributeError("Unable to determine the file's size.")
|
raise AttributeError("Unable to determine the file's size.")
|
||||||
|
|
||||||
|
def _get_size(self):
|
||||||
|
if hasattr(self, '_size'):
|
||||||
|
return self._size
|
||||||
|
self._size = self._get_size_from_underlying_file()
|
||||||
return self._size
|
return self._size
|
||||||
|
|
||||||
def _set_size(self, size):
|
def _set_size(self, size):
|
||||||
|
|
Loading…
Reference in New Issue