Example #1
0
    def close(self):
        # TODO: locking in init, close()
        fname = self.__file.name
        self.__file.close()

        if self.mode in ('w', 'w+', 'r+'):
            par = self._get_parent()
            cr = pooler.get_db(par.context.dbname).cursor()
            icont = ''
            mime = ''
            filename = par.path
            if isinstance(filename, (tuple, list)):
                filename = '/'.join(filename)

            try:
                mime, icont = cntIndex.doIndex(None,
                                               filename=filename,
                                               content_type=None,
                                               realfname=fname)
            except Exception:
                logging.getLogger('document.storage').debug(
                    'Cannot index file:', exc_info=True)
                pass

            try:
                icont_u = ustr(icont)
            except UnicodeError:
                icont_u = ''

            try:
                fsize = os.stat(fname).st_size
                cr.execute("UPDATE ir_attachment " \
                            " SET index_content = %s, file_type = %s, " \
                            " file_size = %s " \
                            "  WHERE id = %s",
                            (icont_u, mime, fsize, par.file_id))
                par.content_length = fsize
                par.content_type = mime
                cr.commit()
                cr.close()
            except Exception:
                logging.getLogger('document.storage').warning(
                    'Cannot save file indexed content:', exc_info=True)

        elif self.mode in ('a', 'a+'):
            try:
                par = self._get_parent()
                cr = pooler.get_db(par.context.dbname).cursor()
                fsize = os.stat(fname).st_size
                cr.execute("UPDATE ir_attachment SET file_size = %s " \
                            "  WHERE id = %s",
                            (fsize, par.file_id))
                par.content_length = fsize
                cr.commit()
                cr.close()
            except Exception:
                logging.getLogger('document.storage').warning(
                    'Cannot save file appended content:', exc_info=True)
    def close(self):
        # TODO: locking in init, close()
        fname = self.__file.name
        self.__file.close()

        if self.mode in ('w', 'w+', 'r+'):
            par = self._get_parent()
            cr = pooler.get_db(par.context.dbname).cursor()
            icont = ''
            mime = ''
            filename = par.path
            if isinstance(filename, (tuple, list)):
                filename = '/'.join(filename)
            
            try:
                mime, icont = cntIndex.doIndex(None, filename=filename,
                        content_type=None, realfname=fname)
            except Exception:
                _logger.debug('Cannot index file:', exc_info=True)
                pass

            try:
                icont_u = ustr(icont)
            except UnicodeError:
                icont_u = ''

            try:
                fsize = os.stat(fname).st_size
                cr.execute("UPDATE ir_attachment " \
                            " SET index_content = %s, file_type = %s, " \
                            " file_size = %s " \
                            "  WHERE id = %s",
                            (icont_u, mime, fsize, par.file_id))
                par.content_length = fsize
                par.content_type = mime
                cr.commit()
                cr.close()
            except Exception:
                _logger.warning('Cannot save file indexed content:', exc_info=True)

        elif self.mode in ('a', 'a+' ):
            try:
                par = self._get_parent()
                cr = pooler.get_db(par.context.dbname).cursor()
                fsize = os.stat(fname).st_size
                cr.execute("UPDATE ir_attachment SET file_size = %s " \
                            "  WHERE id = %s",
                            (fsize, par.file_id))
                par.content_length = fsize
                cr.commit()
                cr.close()
            except Exception:
                _logger.warning('Cannot save file appended content:', exc_info=True)
Example #3
0
    def close(self):
        # TODO: locking in init, close()
        fname = self.__file.name
        self.__file.close()

        if self.mode in ("w", "w+", "r+"):
            par = self._get_parent()
            cr = pooler.get_db(par.context.dbname).cursor()
            icont = ""
            mime = ""
            filename = par.path
            if isinstance(filename, (tuple, list)):
                filename = "/".join(filename)

            try:
                mime, icont = cntIndex.doIndex(None, filename=filename, content_type=None, realfname=fname)
            except Exception:
                logging.getLogger("document.storage").debug("Cannot index file:", exc_info=True)
                pass

            try:
                icont_u = ustr(icont)
            except UnicodeError:
                icont_u = ""

            try:
                fsize = os.stat(fname).st_size
                cr.execute(
                    "UPDATE ir_attachment "
                    " SET index_content = %s, file_type = %s, "
                    " file_size = %s "
                    "  WHERE id = %s",
                    (icont_u, mime, fsize, par.file_id),
                )
                par.content_length = fsize
                par.content_type = mime
                cr.commit()
                cr.close()
            except Exception:
                logging.getLogger("document.storage").warning("Cannot save file indexed content:", exc_info=True)

        elif self.mode in ("a", "a+"):
            try:
                par = self._get_parent()
                cr = pooler.get_db(par.context.dbname).cursor()
                fsize = os.stat(fname).st_size
                cr.execute("UPDATE ir_attachment SET file_size = %s " "  WHERE id = %s", (fsize, par.file_id))
                par.content_length = fsize
                cr.commit()
                cr.close()
            except Exception:
                logging.getLogger("document.storage").warning("Cannot save file appended content:", exc_info=True)
Example #4
0
    def close(self):
        # we now open a *separate* cursor, to update the data.
        # FIXME: this may be improved, for concurrency handling
        par = self._get_parent()
        # uid = par.context.uid
        cr = pooler.get_db(par.context.dbname).cursor()
        try:
            if self.mode in ('w', 'w+', 'r+'):
                data = self.getvalue()
                icont = ''
                mime = ''
                filename = par.path
                if isinstance(filename, (tuple, list)):
                    filename = '/'.join(filename)

                try:
                    mime, icont = cntIndex.doIndex(data,
                                                   filename=filename,
                                                   content_type=None,
                                                   realfname=None)
                except Exception:
                    logging.getLogger('document.storage').debug(
                        'Cannot index file:', exc_info=True)
                    pass

                try:
                    icont_u = ustr(icont)
                except UnicodeError:
                    icont_u = ''

                out = psycopg2.Binary(data)
                cr.execute("UPDATE ir_attachment " \
                            "SET db_datas = %s, file_size=%s, " \
                            " index_content= %s, file_type=%s " \
                            " WHERE id = %s",
                    (out, len(data), icont_u, mime, par.file_id))
            elif self.mode == 'a':
                data = self.getvalue()
                out = psycopg2.Binary(data)
                cr.execute("UPDATE ir_attachment " \
                    "SET db_datas = COALESCE(db_datas,'') || %s, " \
                    "    file_size = COALESCE(file_size, 0) + %s " \
                    " WHERE id = %s",
                    (out, len(data), par.file_id))
            cr.commit()
        except Exception:
            logging.getLogger('document.storage').exception(
                'Cannot update db file #%d for close:', par.file_id)
            raise
        finally:
            cr.close()
        StringIO.close(self)
Example #5
0
    def close(self):
        # we now open a *separate* cursor, to update the data.
        # FIXME: this may be improved, for concurrency handling
        par = self._get_parent()
        # uid = par.context.uid
        cr = pooler.get_db(par.context.dbname).cursor()
        try:
            if self.mode in ("w", "w+", "r+"):
                data = self.getvalue()
                icont = ""
                mime = ""
                filename = par.path
                if isinstance(filename, (tuple, list)):
                    filename = "/".join(filename)

                try:
                    mime, icont = cntIndex.doIndex(data, filename=filename, content_type=None, realfname=None)
                except Exception:
                    logging.getLogger("document.storage").debug("Cannot index file:", exc_info=True)
                    pass

                try:
                    icont_u = ustr(icont)
                except UnicodeError:
                    icont_u = ""

                out = psycopg2.Binary(data)
                cr.execute(
                    "UPDATE ir_attachment "
                    "SET db_datas = %s, file_size=%s, "
                    " index_content= %s, file_type=%s "
                    " WHERE id = %s",
                    (out, len(data), icont_u, mime, par.file_id),
                )
            elif self.mode == "a":
                data = self.getvalue()
                out = psycopg2.Binary(data)
                cr.execute(
                    "UPDATE ir_attachment "
                    "SET db_datas = COALESCE(db_datas,'') || %s, "
                    "    file_size = COALESCE(file_size, 0) + %s "
                    " WHERE id = %s",
                    (out, len(data), par.file_id),
                )
            cr.commit()
        except Exception:
            logging.getLogger("document.storage").exception("Cannot update db file #%d for close:", par.file_id)
            raise
        finally:
            cr.close()
        StringIO.close(self)
Example #6
0
    def close(self):
        # we now open a *separate* cursor, to update the data.
        # FIXME: this may be improved, for concurrency handling
        par = self._get_parent()
        # uid = par.context.uid
        cr = pooler.get_db(par.context.dbname).cursor()
        try:
            if self.mode in ('w', 'w+', 'r+'):
                data = self.getvalue()
                icont = ''
                mime = ''
                filename = par.path
                if isinstance(filename, (tuple, list)):
                    filename = '/'.join(filename)

                try:
                    mime, icont = cntIndex.doIndex(data,
                                                   filename=filename,
                                                   content_type=None,
                                                   realfname=None)
                except Exception:
                    logging.getLogger('document.storage').debug(
                        'Cannot index file:', exc_info=True)
                    pass

                try:
                    icont_u = ustr(icont)
                except UnicodeError:
                    icont_u = ''

                cr.execute('UPDATE ir_attachment SET db_datas = %s::bytea, file_size=%s, ' \
                        'index_content = %s, file_type = %s ' \
                        'WHERE id = %s',
                        (base64.encodestring(data), len(data), icont_u, mime, par.file_id))
            elif self.mode == 'a':
                data = self.getvalue()
                # Yes, we're obviously using the wrong representation for storing our
                # data as base64-in-bytea
                cr.execute("UPDATE ir_attachment " \
                    "SET db_datas = encode( (COALESCE(decode(encode(db_datas,'escape'),'base64'),'') || decode(%s, 'base64')),'base64')::bytea , " \
                    "    file_size = COALESCE(file_size, 0) + %s " \
                    " WHERE id = %s",
                    (base64.encodestring(data), len(data), par.file_id))
            cr.commit()
        except Exception:
            logging.getLogger('document.storage').exception(
                'Cannot update db file #%d for close:', par.file_id)
            raise
        finally:
            cr.close()
        StringIO.close(self)
Example #7
0
    def close(self):
        # we now open a *separate* cursor, to update the data.
        # FIXME: this may be improved, for concurrency handling
        par = self._get_parent()
        # uid = par.context.uid
        cr = pooler.get_db(par.context.dbname).cursor()
        try:
            if self.mode in ("w", "w+", "r+"):
                data = self.getvalue()
                icont = ""
                mime = ""
                filename = par.path
                if isinstance(filename, (tuple, list)):
                    filename = "/".join(filename)

                try:
                    mime, icont = cntIndex.doIndex(data, filename=filename, content_type=None, realfname=None)
                except Exception:
                    logging.getLogger("document.storage").debug("Cannot index file:", exc_info=True)
                    pass

                try:
                    icont_u = ustr(icont)
                except UnicodeError:
                    icont_u = ""

                cr.execute(
                    "UPDATE ir_attachment SET db_datas = %s::bytea, file_size=%s, "
                    "index_content = %s, file_type = %s "
                    "WHERE id = %s",
                    (base64.encodestring(data), len(data), icont_u, mime, par.file_id),
                )
            elif self.mode == "a":
                data = self.getvalue()
                # Yes, we're obviously using the wrong representation for storing our
                # data as base64-in-bytea
                cr.execute(
                    "UPDATE ir_attachment "
                    "SET db_datas = encode( (COALESCE(decode(encode(db_datas,'escape'),'base64'),'') || decode(%s, 'base64')),'base64')::bytea , "
                    "    file_size = COALESCE(file_size, 0) + %s "
                    " WHERE id = %s",
                    (base64.encodestring(data), len(data), par.file_id),
                )
            cr.commit()
        except Exception:
            logging.getLogger("document.storage").exception("Cannot update db file #%d for close:", par.file_id)
            raise
        finally:
            cr.close()
        StringIO.close(self)
    def close(self):
        # we now open a *separate* cursor, to update the data.
        # FIXME: this may be improved, for concurrency handling
        par = self._get_parent()
        # uid = par.context.uid
        cr = pooler.get_db(par.context.dbname).cursor()
        try:
            if self.mode in ('w', 'w+', 'r+'):
                data = self.getvalue()
                icont = ''
                mime = ''
                filename = par.path
                if isinstance(filename, (tuple, list)):
                    filename = '/'.join(filename)

                try:
                    mime, icont = cntIndex.doIndex(data, filename=filename,
                            content_type=None, realfname=None)
                except Exception:
                    _logger.debug('Cannot index file:', exc_info=True)
                    pass

                try:
                    icont_u = ustr(icont)
                except UnicodeError:
                    icont_u = ''

                out = psycopg2.Binary(data)
                cr.execute("UPDATE ir_attachment " \
                            "SET db_datas = %s, file_size=%s, " \
                            " index_content= %s, file_type=%s " \
                            " WHERE id = %s",
                    (out, len(data), icont_u, mime, par.file_id))
            elif self.mode == 'a':
                data = self.getvalue()
                out = psycopg2.Binary(data)
                cr.execute("UPDATE ir_attachment " \
                    "SET db_datas = COALESCE(db_datas,'') || %s, " \
                    "    file_size = COALESCE(file_size, 0) + %s " \
                    " WHERE id = %s",
                    (out, len(data), par.file_id))
            cr.commit()
        except Exception:
            _logger.exception('Cannot update db file #%d for close.', par.file_id)
            raise
        finally:
            cr.close()
        StringIO.close(self)
                raise except_orm(_('Error!'), str(e))

        elif boo.type == 'virtual':
            raise ValueError('Virtual storage does not support static files')

        else:
            raise TypeError("No %s storage" % boo.type)

        # 2nd phase: store the metadata
        try:
            icont = ''
            mime = ira.file_type
            if not mime:
                mime = ""
            try:
                mime, icont = cntIndex.doIndex(data, ira.datas_fname,
                ira.file_type or None, fname)
            except Exception:
                _logger.debug('Cannot index file:', exc_info=True)
                pass

            try:
                icont_u = ustr(icont)
            except UnicodeError:
                icont_u = ''

            # a hack: /assume/ that the calling write operation will not try
            # to write the fname and size, and update them in the db concurrently.
            # We cannot use a write() here, because we are already in one.
            cr.execute('UPDATE ir_attachment SET store_fname = %s, file_size = %s, index_content = %s, file_type = %s WHERE id = %s',
                (store_fname, filesize, icont_u, mime, file_node.file_id))
            file_node.content_length = filesize
Example #10
0
                  default=False,
                  help="delay after the operation, to inspect child processes")

(options, args) = parser.parse_args()

import content_index, std_index

from content_index import cntIndex

for fname in args:
    try:
        if options.docontent:
            fp = open(fname, 'rb')
            content = fp.read()
            fp.close()
            res = cntIndex.doIndex(content, fname, None, None, True)
        else:
            res = cntIndex.doIndex(None, fname, None, fname, True)

        if options.verbose:
            for line in res[:5]:
                print line
        if options.delay:
            time.sleep(30)
    except Exception, e:
        import traceback
        tb_s = reduce(
            lambda x, y: x + y,
            traceback.format_exception(sys.exc_type, sys.exc_value,
                                       sys.exc_traceback))
    except KeyboardInterrupt:
Example #11
0
                raise except_orm(_('Error!'), str(e))

        elif boo.type == 'virtual':
            raise ValueError('Virtual storage does not support static files')

        else:
            raise TypeError("No %s storage" % boo.type)

        # 2nd phase: store the metadata
        try:
            icont = ''
            mime = ira.file_type
            if not mime:
                mime = ""
            try:
                mime, icont = cntIndex.doIndex(data, ira.datas_fname,
                ira.file_type or None, fname)
            except Exception:
                self._doclog.debug('Cannot index file:', exc_info=True)
                pass

            try:
                icont_u = ustr(icont)
            except UnicodeError:
                icont_u = ''

            # a hack: /assume/ that the calling write operation will not try
            # to write the fname and size, and update them in the db concurrently.
            # We cannot use a write() here, because we are already in one.
            cr.execute('UPDATE ir_attachment SET store_fname = %s, file_size = %s, file_type = %s WHERE id = %s',
                (store_fname, filesize, mime, file_node.file_id))
            file_node.content_length = filesize
Example #12
0
    help="delay after the operation, to inspect child processes",
)

(options, args) = parser.parse_args()

import content_index, std_index

from content_index import cntIndex

for fname in args:
    try:
        if options.docontent:
            fp = open(fname, "rb")
            content = fp.read()
            fp.close()
            res = cntIndex.doIndex(content, fname, None, None, True)
        else:
            res = cntIndex.doIndex(None, fname, None, fname, True)

        if options.verbose:
            for line in res[:5]:
                print line
        if options.delay:
            time.sleep(30)
    except Exception, e:
        import traceback

        tb_s = reduce(lambda x, y: x + y, traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
    except KeyboardInterrupt:
        print "Keyboard interrupt"