def _do_add(self, resource, path, author='trac', description=''): realm, id_ = self.split_resource(resource) attachment = Attachment(self.env, realm, id_) attachment.author = author attachment.description = description filename = normalize_filename(os.path.basename(path)) with open(path, 'rb') as f: attachment.insert(filename, f, os.path.getsize(path))
def _getfile(self, upload): filename = fileobj = size = None if hasattr(upload, 'filename'): filename = normalize_filename(upload.filename) if hasattr(upload, 'file'): fileobj = upload.file if hasattr(fileobj, 'fileno'): size = os.fstat(fileobj.fileno())[6] else: fileobj.seek(0, 2) # seek to end of file size = fileobj.tell() fileobj.seek(0) return filename, fileobj, size
def _getfile(self, upload): filename = fileobj = size = None if hasattr(upload, 'filename'): filename = normalize_filename(upload.filename) if hasattr(upload, 'file'): fileobj = upload.file if hasattr(fileobj, 'fileno'): try: size = os.fstat(fileobj.fileno())[6] except io.UnsupportedOperation: size = None if size is None: fileobj.seek(0, 2) # seek to end of file size = fileobj.tell() fileobj.seek(0) return filename, fileobj, size