Example #1
0
def makeThumbNail(image, thumb):
    if isnewer(thumb, image):
        return
    pic = PILImage.open(image)
    tmpjpg = config.get("paths.datadir") + "tmp/img" + str(
        random.random()) + ".jpg"

    if pic.mode == "CMYK" and (image.endswith("jpg")
                               or image.endswith("jpeg")) or pic.mode in [
                                   "P", "L"
                               ]:
        os.system("convert -quality 100 -draw \"rectangle 0,0 1,1\" %s %s" %
                  (image, tmpjpg))  # always get a rgb image
        pic = PILImage.open(tmpjpg)

    try:
        pic.load()
    except IOError as e:
        pic = None
        raise OperationException("error:" + str(e))

    width = pic.size[0]
    height = pic.size[1]
    if width > height:
        newwidth = 128
        newheight = height * newwidth / width
    else:
        newheight = 128
        newwidth = width * newheight / height
    pic = pic.resize((newwidth, newheight), PILImage.ANTIALIAS)
    try:
        im = PILImage.new(pic.mode, (128, 128), (255, 255, 255))
    except:
        im = PILImage.new("RGB", (128, 128), (255, 255, 255))

    x = (128 - newwidth) / 2
    y = (128 - newheight) / 2
    im.paste(pic, (x, y, x + newwidth, y + newheight))

    draw = ImageDraw.ImageDraw(im)
    draw.line([(0, 0), (127, 0), (127, 127), (0, 127), (0, 0)],
              (128, 128, 128))

    im = im.convert("RGB")
    im.save(thumb, "jpeg")
    if os.path.exists(tmpjpg):
        os.unlink(tmpjpg)
Example #2
0
def makePresentationFormat(image, thumb):
    if isnewer(thumb, image):
        return
    pic = PILImage.open(image)
    tmpjpg = config.get("paths.datadir") + "tmp/img" + str(
        random.random()) + ".jpg"
    if pic.mode == "CMYK" and (image.endswith("jpg")
                               or image.endswith("jpeg")) or pic.mode in [
                                   "P", "L"
                               ]:
        os.system("convert -quality 100 -draw \"rectangle 0,0 1,1\" %s %s" %
                  (image, tmpjpg))
        pic = PILImage.open(tmpjpg)

    try:
        pic.load()
    except IOError as e:
        pic = None
        raise OperationException("error:" + str(e))

    width = pic.size[0]
    height = pic.size[1]

    resize = 1
    if resize:
        # resize images only if they are actually too big
        if width > height:
            newwidth = 320
            newheight = height * newwidth / width
        else:
            newheight = 320
            newwidth = width * newheight / height
        pic = pic.resize((newwidth, newheight), PILImage.ANTIALIAS)

    try:
        pic.save(thumb, "jpeg")
    except IOError:
        pic.convert('RGB').save(thumb, "jpeg")

    if os.path.exists(tmpjpg):
        os.unlink(tmpjpg)
Example #3
0
    def __init__(self):
        self.connames = {
            'std': {
                'full': 'std',
                'ext': 'std',
                'text': 'std',
                'std': 'std'
            },
            'split': {
                'full': 'full',
                'ext': 'ext',
                'text': 'text'
            }
        }
        self.tablenames = {
            'full': "fullsearchmeta",
            'ext': "searchmeta",
            'text': "textsearchmeta"
        }
        self.schemas = list(
            set([
                schema[0].split('/')[1]
                for schema in tree.db.runQuery('''select distinct type
                                            from node
                                            where type
                                            like "%%/%%"''')
            ]))
        self.db = {}

        if DBTYPE not in self.connames.keys():
            raise OperationException("error in search definition")

        # fills in db with key {schema}_{dbtype}: sqliteconnector
        for schema in self.schemas:
            for conname in self.connames[DBTYPE]:
                self.addDB(schema, conname)

        self.normalization_items = None
Example #4
0
def makeOriginalFormat(image, thumb):

    tmpjpg = config.get("paths.datadir") + "tmp/img" + str(
        random.random()) + ".jpg"
    pic = PILImage.open(image)
    if pic.mode == "CMYK" and (image.endswith("jpg")
                               or image.endswith("jpeg")) or pic.mode in [
                                   "P", "L"
                               ]:
        # if image.endswith("jpg") or image.endswith("jpeg"):
        os.system("convert -quality 100 -draw \"rectangle 0,0 1,1\" %s %s" %
                  (image, tmpjpg))
        pic = PILImage.open(tmpjpg)

    try:
        pic.load()
    except IOError as e:
        pic = None
        raise OperationException("error:" + str(e))

    pic.save(thumb, "png")
    if os.path.exists(tmpjpg):
        os.unlink(tmpjpg)
Example #5
0
def upload_new_node(req, path, params, data):

    try:
        uploadfile = params['data']
        del params['data']
    except KeyError:
        uploadfile = None

    # get the user and verify the signature
    if params.get('user'):
        # user=users.getUser(params.get('user'))
        #userAccess = AccessData(user=user)
        _user = users.getUser(params.get('user'))
        if not _user:  # user of dynamic

            class dummyuser:  # dummy user class

                # return all groups with given dynamic user
                def getGroups(self):
                    return [
                        g.name
                        for g in tree.getRoot('usergroups').getChildren()
                        if g.get('allow_dynamic') == '1'
                        and params.get('user') in g.get('dynamic_users')
                    ]

                def getName(self):
                    return params.get('user')

                def getDirID(self):  # unique identifier
                    return params.get('user')

                def isAdmin(self):
                    return 0

            _user = dummyuser()
        userAccess = AccessData(user=_user)

        if userAccess.user:
            user = userAccess.user
            if not userAccess.verify_request_signature(req.fullpath + '?',
                                                       params):
                userAccess = None
        else:
            userAccess = None
    else:
        user = users.getUser(config.get('user.guestuser'))
        userAccess = AccessData(user=user)

    parent = tree.getNode(params.get('parent'))

    # check user access
    if userAccess and userAccess.hasAccess(parent, "write"):
        pass
    else:
        s = "No Access"
        req.write(s)
        d = {
            'status': 'fail',
            'html_response_code': '403',
            'errormessage': 'no access'
        }
        logger.error("user has no edit permission for node %s" % parent)
        return d['html_response_code'], len(s), d

    datatype = params.get('type')
    uploaddir = users.getUploadDir(user)

    n = tree.Node(name=params.get('name'), type=datatype)
    if isinstance(uploadfile, types.InstanceType):  # file object used
        nfile = importFile(uploadfile.filename, uploadfile.tempname)
    else:  # string used
        nfile = importFileFromData('uploadTest.jpg',
                                   base64.b64decode(uploadfile))
    if nfile:
        n.addFile(nfile)
    else:
        logger.error("error in file uploadservice")

    try:  # test metadata
        metadata = json.loads(params.get('metadata'))
    except ValueError:
        metadata = dict()

    # set provided metadata
    for key, value in metadata.iteritems():
        n.set(u(key), u(value))

    # service flags
    n.set("creator", user.getName())
    n.set("creationtime", format_date())

    parent.addChild(n)

    # process the file, we've added to the new node
    if hasattr(n, "event_files_changed"):
        try:
            n.event_files_changed()

        except OperationException as e:
            for file in n.getFiles():
                if os.path.exists(file.retrieveFile()):
                    os.remove(file.retrieveFile())
            raise OperationException(e.value)

    # make sure the new node is visible immediately from the web service and
    # the search index gets updated
    n.setDirty()
    tree.remove_from_nodecaches(parent)

    d = {
        'status': 'Created',
        'html_response_code': '201',
        'build_response_end': time.time()
    }
    s = "Created"

    # provide the uploader with the new node ID
    req.reply_headers['NodeID'] = n.id

    # we need to write in case of POST request, send as buffer will not work
    req.write(s)

    return d['html_response_code'], len(s), d
Example #6
0
    def event_files_changed(self):
        print "Postprocessing node", self.id

        thumb = 0
        fulltext = 0
        doc = None
        present = 0
        fileinfo = 0
        for f in self.getFiles():
            if f.type == "thumb":
                thumb = 1
            elif f.type.startswith("present"):
                present = 1
            elif f.type == "fulltext":
                fulltext = 1
            elif f.type == "fileinfo":
                fileinfo = 1
            elif f.type == "doc":
                doc = f
            elif f.type == "document":
                doc = f
        if not doc:
            for f in self.getFiles():
                if f.type == "thumb":
                    self.removeFile(f)
                elif f.type.startswith("present"):
                    self.removeFile(f)
                elif f.type == "fileinfo":
                    self.removeFile(f)
                elif f.type == "fulltext":
                    self.removeFile(f)

        #fetch unwated tags to be omitted
        unwanted_attrs = self.unwanted_attributes()

        if doc:
            path, ext = splitfilename(doc.retrieveFile())

            if not (thumb and present and fulltext and fileinfo):
                thumbname = path + ".thumb"
                thumb2name = path + ".thumb2"
                fulltextname = path + ".txt"
                infoname = path + ".info"
                tempdir = config.get("paths.tempdir")

                try:
                    pdfdata = parsepdf.parsePDF2(doc.retrieveFile(), tempdir)
                except parsepdf.PDFException as ex:
                    raise OperationException(ex.value)
                fi = open(infoname, "rb")
                for line in fi.readlines():
                    i = line.find(':')
                    if i > 0:
                        if any(tag in line[0:i].strip().lower()
                               for tag in unwanted_attrs):
                            continue
                        self.set("pdf_" + line[0:i].strip().lower(),
                                 utf8_decode_escape(line[i + 1:].strip()))
                fi.close()
                self.addFile(
                    FileNode(name=thumbname,
                             type="thumb",
                             mimetype="image/jpeg"))
                self.addFile(
                    FileNode(name=thumb2name,
                             type="presentation",
                             mimetype="image/jpeg"))
                self.addFile(
                    FileNode(name=fulltextname,
                             type="fulltext",
                             mimetype="text/plain"))
                self.addFile(
                    FileNode(name=infoname,
                             type="fileinfo",
                             mimetype="text/plain"))
Example #7
0
    def event_files_changed(self):
        logg.debug("Postprocessing node %s", self.id)

        thumb = 0
        fulltext = 0
        doc = None
        present = 0
        fileinfo = 0
        for f in self.files:
            if f.type == "thumb":
                thumb = 1
            elif f.type.startswith("present"):
                present = 1
            elif f.type == "fulltext":
                fulltext = 1
            elif f.type == "fileinfo":
                fileinfo = 1
            elif f.type == "document":
                doc = f
        if not doc:
            for f in self.files:
                if f.type == "thumb":
                    self.files.remove(f)
                elif f.type.startswith("present"):
                    self.files.remove(f)
                elif f.type == "fileinfo":
                    self.files.remove(f)
                elif f.type == "fulltext":
                    self.files.remove(f)

        #fetch unwanted tags to be omitted
        unwanted_attrs = self.get_unwanted_exif_attributes()

        if doc:
            path, ext = splitfilename(doc.abspath)

            if not (thumb and present and fulltext and fileinfo):
                thumbname = path + ".thumb"
                thumb2name = path + ".thumb2"
                fulltextname = path + ".txt"
                infoname = path + ".info"
                tempdir = config.get("paths.tempdir")

                try:
                    pdfdata = parsepdf.parsePDFExternal(doc.abspath, tempdir)
                except parsepdf.PDFException as ex:
                    if ex.value == 'error:document encrypted':
                        # allow upload of encrypted document
                        db.session.commit()
                        return
                    raise OperationException(ex.value)
                with codecs.open(infoname, "rb", encoding='utf8') as fi:
                    for line in fi.readlines():
                        i = line.find(':')
                        if i > 0:
                            if any(tag in line[0:i].strip().lower()
                                   for tag in unwanted_attrs):
                                continue
                            self.set("pdf_" + line[0:i].strip().lower(),
                                     utf8_decode_escape(line[i + 1:].strip()))

                self.files.append(File(thumbname, "thumb", "image/jpeg"))
                self.files.append(
                    File(thumb2name, "presentation", "image/jpeg"))
                self.files.append(File(fulltextname, "fulltext", "text/plain"))
                self.files.append(File(infoname, "fileinfo", "text/plain"))

        if doc:
            import_node_fulltext(self, overwrite=True)

        db.session.commit()