Exemplo n.º 1
0
    def create_file_from_request(self, name):
        context = self.context
        filedata = self.request.form.get(name, None)
        if not filedata:
            return
        filename = filedata.filename
        content_type = mimetypes.guess_type(filename)[0] or ""
        # Determine if the default file/image types are DX or AT based
        ctr = api.portal.get_tool('content_type_registry')
        type_ = ctr.findTypeName(filename.lower(), '', '') or 'File'
        pt = api.portal.get_tool('portal_types')

        obj = IDXFileFactory(context)(filename, content_type, filedata)
        if hasattr(obj, 'file'):
            size = obj.file.getSize()
            content_type = obj.file.contentType
        elif hasattr(obj, 'image'):
            size = obj.image.getSize()
            content_type = obj.image.contentType
        else:
            return
        result = {
            "type": content_type,
            "size": size
        }
        result.update({
            'url': obj.absolute_url(),
            'name': obj.getId(),
            'UID': IUUID(obj),
            'filename': filename
        })
        return result    
##/code-section bottom
Exemplo n.º 2
0
    def __call__(self):
        body = json.loads(self.request.get('BODY'))
        url = body.get('url')
        fileType = body.get('fileType')
        fileTitle = body.get('fileTitle')
        folderUID = body.get('folderUID')

        if not url or not fileType or not fileTitle:
            raise BadRequest(
                u'Required url or fileType or fileTitle parameters not found.')

        if not folderUID:
            portal_url = getToolByName(self.context, "portal_url")
            folder = portal_url.getPortalObject()
        else:
            folder = uuidToObject(folderUID)

        if not getSecurityManager().checkPermission(AddPortalContent, folder):
            response = self.request.RESPONSE
            response.setStatus(403)
            return "You are not authorized to add content to this folder."

        fileName = fileUtils.getCorrectFileName(fileTitle + "." + fileType)
        contentType = mimetypes.guess_type(fileName)[0] or ''

        data = urlopen(url).read()

        factory = IDXFileFactory(folder)
        file = factory(fileName, contentType, data)

        self.request.response.setHeader("Content-Type",
                                        "application/json; charset=utf-8")

        return json_dumps({"status": "success", "fileName": fileName})
Exemplo n.º 3
0
    def __call__(self, documentType):
        fileName = fileUtils.getDefaultNameByType(documentType)
        fileExt = fileUtils.getDefaultExtByType(documentType)

        if fileName is None or fileExt is None:
            raise NotFound(self, documentType, self.request)

        template = 'new.' + fileExt

        state = portal_state(self)
        language = state.language()

        localePath = fileUtils.localePath.get(language)
        if localePath is None:
            language = language.split('-')[0]
            localePath = fileUtils.localePath.get(language)
            if localePath is None:
                localePath = fileUtils.localePath.get('en')

        file = open(
            os.path.join(os.path.abspath(os.path.dirname(__file__)),
                         'app_data', localePath, template), 'rb')

        try:
            data = file.read()
        finally:
            file.close()

        factory = IDXFileFactory(self.context)
        contentType = mimetypes.guess_type(template)[0] or ''

        file = factory(fileName + '.' + fileExt, contentType, data)

        self.request.response.redirect(
            addTokenToUrl('{0}/onlyoffice-edit'.format(file.absolute_url())))
Exemplo n.º 4
0
    def create_file_from_request(self, name):
        context = self.context
        filedata = self.request.form.get(name, None)
        if not filedata:
            return
        filename = filedata.filename
        content_type = mimetypes.guess_type(filename)[0] or ""
        # Determine if the default file/image types are DX or AT based
        ctr = api.portal.get_tool('content_type_registry')
        type_ = ctr.findTypeName(filename.lower(), '', '') or 'File'
        pt = api.portal.get_tool('portal_types')

        if IDexterityFTI.providedBy(getattr(pt, type_)):
            obj = IDXFileFactory(context)(filename, content_type, filedata)
            if hasattr(obj, 'file'):
                size = obj.file.getSize()
                content_type = obj.file.contentType
            elif hasattr(obj, 'image'):
                size = obj.image.getSize()
                content_type = obj.image.contentType
            else:
                return
            result = {
                "type": content_type,
                "size": size
            }
        else:
            from Products.ATContentTypes.interfaces import IATCTFileFactory
            obj = IATCTFileFactory(context)(filename, content_type, filedata)
            try:
                size = obj.getSize()
            except AttributeError:
                size = obj.getObjSize()
            result = {
                "type": obj.getContentType(),
                "size": size
            }
        result.update({
            'url': obj.absolute_url(),
            'name': obj.getId(),
            'UID': IUUID(obj),
            'filename': filename
        })
        return result
Exemplo n.º 5
0
    def create_file_from_request(self, name):
        context = self.context
        filedata = self.request.form.get(name, None)
        if not filedata:
            return
        filename = filedata.filename
        content_type = mimetypes.guess_type(filename)[0] or ""
        # Determine if the default file/image types are DX or AT based
        ctr = api.portal.get_tool('content_type_registry')
        type_ = ctr.findTypeName(filename.lower(), '', '') or 'File'
        pt = api.portal.get_tool('portal_types')

        if IDexterityFTI.providedBy(getattr(pt, type_)):
            obj = IDXFileFactory(context)(filename, content_type, filedata)
            if hasattr(obj, 'file'):
                size = obj.file.getSize()
                content_type = obj.file.contentType
            elif hasattr(obj, 'image'):
                size = obj.image.getSize()
                content_type = obj.image.contentType
            else:
                return
            result = {
                "type": content_type,
                "size": size
            }
        else:
            from Products.ATContentTypes.interfaces import IATCTFileFactory
            obj = IATCTFileFactory(context)(filename, content_type, filedata)
            try:
                size = obj.getSize()
            except AttributeError:
                size = obj.getObjSize()
            result = {
                "type": obj.getContentType(),
                "size": size
            }
        result.update({
            'url': obj.absolute_url(),
            'name': obj.getId(),
            'UID': IUUID(obj),
            'filename': filename
        })
        return result
Exemplo n.º 6
0
    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)
Exemplo n.º 7
0
    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
        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)