def test_tus_create(self): from tus import Tus, Zope2RequestAdapter req = self.request req.REQUEST_METHOD = 'POST' req.environ['ENTITY_LENGTH'] = '12345' tus = Tus(Zope2RequestAdapter(req), **self.options) assert tus.valid tus.handle() assert req.response.code == 201
def test_tus_create(self): from tus import Tus, WebobAdapter req = Request.blank('/', method='POST') adapter = WebobAdapter(req) req.headers['ENTITY_LENGTH'] = '12345' tus = Tus(adapter, **self.options) assert tus.valid tus.handle() assert adapter.resp.status_code == 201
def test_tus_multi_part_upload(self): from tus import Tus, WebobAdapter req = Request.blank('/', method='POST') adapter = WebobAdapter(req) fi = open(test_file) length = os.path.getsize(test_file) req.headers['ENTITY_LENGTH'] = str(length * 2) tus = Tus(adapter, **self.options) tus.handle() req = Request.blank('/' + adapter.resp.headers.get('Location'), method='PATCH') req.body_file = fi req.headers['CONTENT_LENGTH'] = str(length) req.headers['OFFSET'] = '0' adapter = WebobAdapter(req) tus = Tus(adapter, **self.options) tus.handle() assert not tus.upload_finished # twice req.headers['OFFSET'] = str(length) tus.handle() assert tus.upload_finished fipath = tus.get_filepath() otherfi = open(fipath) fi.seek(0) self.assertEquals(len(fi.read()) * 2, len(otherfi.read())) fi.close() otherfi.close()
def test_tus_multi_part_upload(self): from tus import Tus, Zope2RequestAdapter req = self.request fi = open(test_file) req.REQUEST_METHOD = 'POST' length = os.path.getsize(test_file) req.environ['ENTITY_LENGTH'] = str(length * 2) tus = Tus(Zope2RequestAdapter(req), **self.options) tus.handle() req.REQUEST_METHOD = 'PATCH' req._file = fi req.URL = req.response.getHeader('Location') req.environ['CONTENT_LENGTH'] = str(length) req.environ['OFFSET'] = '0' tus.handle() assert not tus.upload_finished # twice req.environ['OFFSET'] = str(length) tus.handle() assert tus.upload_finished fipath = tus.get_filepath() otherfi = open(fipath) fi.seek(0) self.assertEquals(len(fi.read()) * 2, len(otherfi.read())) fi.close() otherfi.close()
def __call__(self): req = self.request tusrequest = False if TUS_ENABLED: adapter = Zope2RequestAdapter(req) tus = Tus(adapter, **tus_settings) if tus.valid: tusrequest = True tus.handle() if not tus.upload_finished: return else: filename = req.getHeader('FILENAME') if tus.send_file: filedata = req._file filedata.filename = filename else: filepath = req._file.read() filedata = open(filepath) if not tusrequest: if req.REQUEST_METHOD != 'POST': return filedata = self.request.form.get("file", None) if filedata is None: return filename = filedata.filename content_type = mimetypes.guess_type(filename)[0] or "" if not filedata: return # Determine if the default file/image types are DX or AT based ctr = getToolByName(self.context, 'content_type_registry') type_ = ctr.findTypeName(filename.lower(), '', '') or 'File' DX_BASED = False if HAS_DEXTERITY: pt = getToolByName(self.context, 'portal_types') if IDexterityFTI.providedBy(getattr(pt, type_)): factory = IDXFileFactory(self.context) DX_BASED = True else: factory = IATCTFileFactory(self.context) else: factory = IATCTFileFactory(self.context) obj = factory(filename, content_type, filedata) if DX_BASED: if 'File' in obj.portal_type: size = obj.file.getSize() content_type = obj.file.contentType elif 'Image' in obj.portal_type: size = obj.image.getSize() content_type = obj.image.contentType result = { "type": content_type, "size": size } else: try: size = obj.getSize() except AttributeError: size = obj.getObjSize() result = { "type": obj.getContentType(), "size": size } if tusrequest: tus.cleanup_file() result.update({ 'url': obj.absolute_url(), 'name': obj.getId(), 'UID': IUUID(obj), 'filename': filename }) return json.dumps(result)
def __call__(self): req = self.request tusrequest = False if TUS_ENABLED: adapter = Zope2RequestAdapter(req) tus = Tus(adapter, **tus_settings) if tus.valid: tusrequest = True tus.handle() if not tus.upload_finished: return else: filename = req.getHeader('FILENAME') if tus.send_file: filedata = req._file filedata.filename = filename else: filepath = req._file.read() filedata = open(filepath) if not tusrequest: if req.REQUEST_METHOD != 'POST': return filedata = self.request.form.get("file", None) if filedata is None: return filename = filedata.filename content_type = mimetypes.guess_type(filename)[0] or "" if not filedata: return # Determine if the default file/image types are DX or AT based ctr = getToolByName(self.context, 'content_type_registry') type_ = ctr.findTypeName(filename.lower(), '', '') or 'File' DX_BASED = False if HAS_DEXTERITY: pt = getToolByName(self.context, 'portal_types') if IDexterityFTI.providedBy(getattr(pt, type_)): factory = IDXFileFactory(self.context) DX_BASED = True else: factory = IATCTFileFactory(self.context) else: factory = IATCTFileFactory(self.context) obj = factory(filename, content_type, filedata) if DX_BASED: if 'File' in obj.portal_type: size = obj.file.getSize() content_type = obj.file.contentType elif 'Image' in obj.portal_type: size = obj.image.getSize() content_type = obj.image.contentType result = {"type": content_type, "size": size} else: try: size = obj.getSize() except AttributeError: size = obj.getObjSize() result = {"type": obj.getContentType(), "size": size} if tusrequest: tus.cleanup_file() result.update({ 'url': obj.absolute_url(), 'name': obj.getId(), 'UID': IUUID(obj), 'filename': filename }) return json.dumps(result)
def __call__(self): # Check if user has permission to add content here sm = getSecurityManager() if not sm.checkPermission(AddPortalContent, self.context): response = self.request.RESPONSE response.setStatus(403) return "You are not authorized to add content to this folder." req = self.request tusrequest = False if TUS_ENABLED: adapter = Zope2RequestAdapter(req) tus = Tus(adapter, **tus_settings) if tus.valid: tusrequest = True tus.handle() if not tus.upload_finished: return else: filename = req.getHeader('FILENAME') if tus.send_file: filedata = req._file filedata.filename = filename else: filepath = req._file.read() filedata = open(filepath) if not tusrequest: if req.REQUEST_METHOD != 'POST': return filedata = self.request.form.get("file", None) if filedata is None: return filename = filedata.filename content_type = mimetypes.guess_type(filename)[0] or "" if not filedata: return # Determine if the default file/image types are DX or AT based ctr = getToolByName(self.context, 'content_type_registry') type_ = ctr.findTypeName(filename.lower(), '', '') or 'File' # Now check that the object is not restricted to be added in the # current context allowed_ids = [ fti.getId() for fti in self.context.allowedContentTypes() ] if type_ not in allowed_ids: response = self.request.RESPONSE response.setStatus(403) if type_ == 'File': return "You cannot add a File to this folder, try another one" if type_ == 'Image': return "You cannot add an Image to this folder, try another one" dx_based = False pt = getToolByName(self.context, 'portal_types') if IDexterityFTI.providedBy(getattr(pt, type_)): factory = IDXFileFactory(self.context) dx_based = True else: from Products.ATContentTypes.interfaces import IATCTFileFactory factory = IATCTFileFactory(self.context) obj = factory(filename, content_type, filedata) if dx_based: if 'File' in obj.portal_type: size = obj.file.getSize() content_type = obj.file.contentType elif 'Image' in obj.portal_type: size = obj.image.getSize() content_type = obj.image.contentType result = {"type": content_type, "size": size} else: try: size = obj.getSize() except AttributeError: size = obj.getObjSize() result = {"type": obj.getContentType(), "size": size} if tusrequest: tus.cleanup_file() result.update({ 'url': obj.absolute_url(), 'name': obj.getId(), 'UID': IUUID(obj), 'filename': filename }) return json.dumps(result)