예제 #1
0
def serve_file(req, path, params, data, filepath):
    atime = starttime = time.time()

    d = {'timetable': []}
    if 'mimetype' in req.params:
        mimetype = req.params['mimetype']
    elif filepath.lower().endswith('.html') or filepath.lower().endswith('.htm'):
        mimetype = 'text/html'
    else:
        mimetype = getMimeType(filepath)

    req.reply_headers['Content-Type'] = mimetype

    if WEBROOT:
        basedir = WEBROOT
    else:
        basedir = os.path.dirname(os.path.abspath(__file__))
    abspath = os.path.join(basedir, 'static', filepath)
    logger.info("web service trying to serve: %s" % abspath)
    if os.path.isfile(abspath):
        filesize = os.path.getsize(abspath)
        req.sendFile(abspath, mimetype, force=1)
        d['timetable'].append(
            ["reading file '%s'" % filepath, time.time() - atime])
        d['status'] = 'ok'
        d['dataready'] = "%.3f" % (time.time() - starttime)
        return 200, filesize, d  # ok
    else:
        d['status'] = 'fail'
        d['dataready'] = "%.3f" % (time.time() - starttime)
        return 404, 0, d  # not found
예제 #2
0
파일: video.py 프로젝트: agromsl/mediatum
    def show_node_big(self, req, template="", macro=""):
        if len([f.retrieveFile() for f in self.getFiles()]) == 0:
            styles = getContentStyles("bigview", contenttype=self.getContentType())
            template = styles[0].getTemplate()
            return req.getTAL(template, self._prepareData(req), macro)

        if template == "":
            styles = getContentStyles("bigview", contenttype=self.getContentType())
            if len(styles) >= 1:
                template = styles[0].getTemplate()

        captions_info = getCaptionInfoDict(self)

        if captions_info:
            logger.info("video: '%s' (%s): captions: dictionary 'captions_info': %s" % (self.name, str(self.id), str(captions_info)))

        context = self._prepareData(req)
        context["captions_info"] = json.dumps(captions_info)

        if len([m for m in [getMimeType(os.path.abspath(f.retrieveFile()))[0].split('/')[0] for f in self.getFiles() if os.path.exists(os.path.abspath(f.retrieveFile()))] if m == 'video'])>0:
            return req.getTAL(template, context, macro)
        else:
            if len([th for th in [os.path.abspath(f.retrieveFile()) for f in self.getFiles()] if th.endswith('thumb2')])>0:
                return req.getTAL(template, context, macro)
            else:
                if len([f for f in self.getFiles() if f.getType() == 'attachment'])>0:
                    return req.getTAL(template, context, macro)

                return '<h2 style="width:100%; text-align:center ;color:red">Video is not in a supported video-format.</h2>'
예제 #3
0
파일: upload.py 프로젝트: agromsl/mediatum
def upload_filehandler(req):
    mime = getMimeType(req.params.get('file'))
    scheme_type = {mime[1]: []}
    for scheme in getSchemes(req):
        if mime[1] in scheme.getDatatypes():
            scheme_type[mime[1]].append(scheme)
            # break

    return {'files': [req.params.get('file')], 'schemes': scheme_type}
예제 #4
0
파일: streams.py 프로젝트: hibozzy/mediatum
def send_image_watermark(req):
    try:
        result = splitpath(req.path)
        n = tree.getNode(result[0])
    except tree.NoSuchNodeError:
        return 404
    for f in n.getFiles():
        if f.getType() == "original_wm":
            return req.sendFile(f.retrieveFile(), getMimeType(f.retrieveFile()))
    return 404
예제 #5
0
def send_from_webroot(req):
    import core.webconfig

    if core.webconfig.theme is not None:
        webroot_dirs = [core.webconfig.theme.path, "web/root"]
    else:
        webroot_dirs = ["web/root"]
    
    for webroot_dir in webroot_dirs:
        filepath = os.path.join(config.basedir, webroot_dir, req.path.strip("/"))
        if os.path.isfile(filepath):
            return req.sendFile(filepath, getMimeType(filepath)[0])

    return 404
예제 #6
0
파일: files.py 프로젝트: mediatum/mediatum
def _finish_change(node, change_file, user, uploadfile, req):


    if change_file in ["yes", "no"]:

        # check that the correct filetype is uploaded
        # note: only the suffix of the filename is checked not the file content
        uploadfile_type = getMimeType(uploadfile.filename)[1]
        if uploadfile_type != node.type and uploadfile_type != node.get_upload_filetype():
            req.setStatus(httpstatus.HTTP_NOT_ACCEPTABLE)
            return

        # sys files are always cleared to delete remaining thumbnails, presentation images etc.
        for f in node.files:
            if f.filetype in node.get_sys_filetypes():
                node.files.remove(f)

        file = importFile(uploadfile.filename, uploadfile.tempname)  # add new file
        file.filetype = node.get_upload_filetype()
        node.files.append(file)
        # this should re-create all dependent files
        node.event_files_changed()
        logg.info(u"%s changed file of node %s to %s (%s)", user.login_name, node.id, uploadfile.filename, uploadfile.tempname)
        return

    attpath = ""
    for f in node.files:
        if f.mimetype == "inode/directory":
            attpath = f.base_name
            break

    if change_file == "attdir":  # add attachmentdir
        if attpath == "":  # add attachment directory
            attpath = req.params.get("inputname")
            if not os.path.exists(getImportDir() + "/" + attpath):
                os.mkdir(getImportDir() + "/" + attpath)
                node.files.append(File(getImportDir() + "/" + attpath, "attachment", "inode/directory"))

            importFileIntoDir(getImportDir() + "/" + attpath, uploadfile.tempname)  # add new file

    if change_file == "attfile":  # add file as attachment
        if attpath == "":
            # no attachment directory existing
            file = importFileToRealname(uploadfile.filename, uploadfile.tempname)  # add new file
            file.mimetype = "inode/file"
            file.filetype = "attachment"
            node.files.append(file)
        else:
            # import attachment file into existing attachment directory
            importFileIntoDir(getImportDir() + "/" + attpath, uploadfile.tempname)  # add new file
예제 #7
0
파일: upload.py 프로젝트: agromsl/mediatum
def upload_ziphandler(req):
    schemes = getSchemes(req)
    files = []
    scheme_type = {}
    basenode = tree.getNode(req.params.get('id'))
    for file in basenode.getFiles():
        if file.retrieveFile().endswith(req.params.get('file')):
            z = zipfile.ZipFile(file.retrieveFile())
            for f in z.namelist():
                #strip unwanted garbage from string
                name = mybasename(f).decode('utf8', 'ignore').encode('utf8')
                random_str = str(random.random())[2:]
                if name.startswith("._"):  # ignore Mac OS X junk
                    continue
                if name.split('.')[0] == '':
                    name = random_str + name

                files.append(name.replace(" ", "_"))
                _m = getMimeType(name)

                if random_str in name:
                    newfilename = join_paths(config.get("paths.tempdir"), name.replace(" ", "_"))
                else:
                    newfilename = join_paths(config.get("paths.tempdir"),  random_str + name.replace(" ", "_"))

                fi = open(newfilename, "wb")
                fi.write(z.read(f))
                fi.close()

                fn = importFileToRealname(mybasename(name.replace(" ", "_")), newfilename)
                basenode.addFile(fn)
                if os.path.exists(newfilename):
                    os.unlink(newfilename)

                if _m[1] not in scheme_type:
                    scheme_type[_m[1]] = []
                    for scheme in schemes:
                        if _m[1] in scheme.getDatatypes():
                            scheme_type[_m[1]].append(scheme)
            try:
                z.close()
                os.remove(file.retrieveFile())
            except:
                pass
            basenode.removeFile(file)
    return {'files': files, 'schemes': scheme_type}
예제 #8
0
def send_attfile(req):
    """send single attachment file to user"""
    parts = req.path[9:].split('/')

    if len(parts) < 2:
        return 400

    nid = userinput.string_to_int(parts[0])
    if nid is None:
        return 400

    version_id = version_id_from_req(req)

    node = get_node_or_version(nid, version_id, Data)

    # XXX: why do we want to send attachments from containers?
    if (node is None
            or isinstance(node, Container) and not node.has_read_access()
            or isinstance(node, Content) and not node.has_data_access()):
        return 404

    paths = ["/".join(parts[1:]), "/".join(parts[1:-1])]
    fileobjs = [fo for fo in node.files if fo.path in paths]

    if not fileobjs:
        return 404

    fileobj = fileobjs[0]

    if fileobj.mimetype == u'inode/directory':
        # files in attachment directory cannot be found in node.files
        # so send file directly as it was made in mysql
        filename = clean_path("/".join(parts[1:]))
        path = os.path.join(config.get("paths.datadir"), filename)
        mime, type = getMimeType(filename)
        if (get_filesize(filename) > 16 * 1048576):
            req.reply_headers["Content-Disposition"] = 'attachment; filename="{}"'.format(filename)

        return req.sendFile(path, mime)

    if (fileobj.size > 16 * 1048576):
        req.reply_headers["Content-Disposition"] = u'attachment; filename="{}"'.format(fileobj.base_name).encode('utf8')

    return req.sendFile(fileobj.abspath, fileobj.mimetype)
예제 #9
0
파일: upload.py 프로젝트: mediatum/mediatum
def upload_ziphandler(req):
    schemes = get_permitted_schemas()
    files = []
    scheme_type = {}
    basenode = q(Node).get(req.params.get('id'))
    for file in basenode.files:
        if file.abspath.endswith(req.params.get('file')):
            z = zipfile.ZipFile(file.abspath)
            for f in z.namelist():
                #strip unwanted garbage from string
                name = mybasename(f).decode('utf8', 'ignore').encode('utf8')
                random_str = ustr(random.random())[2:]
                if name.startswith("._"):  # ignore Mac OS X junk
                    continue
                if name.split('.')[0] == '':
                    name = random_str + name

                files.append(name.replace(" ", "_"))
                _m = getMimeType(name)

                if random_str in name:
                    newfilename = join_paths(config.get("paths.tempdir"), name.replace(" ", "_"))
                else:
                    newfilename = join_paths(config.get("paths.tempdir"),  random_str + name.replace(" ", "_"))

                with codecs.open(newfilename, "wb") as fi:
                    fi.write(z.read(f))

                fn = importFileToRealname(mybasename(name.replace(" ", "_")), newfilename)
                basenode.files.append(fn)
                if os.path.exists(newfilename):
                    os.unlink(newfilename)

                if _m[1] not in scheme_type:
                    scheme_type[_m[1]] = []
                    for scheme in schemes:
                        if _m[1] in scheme.getDatatypes():
                            scheme_type[_m[1]].append(scheme)
            with suppress(Exception, warn=False):
                z.close()
                os.remove(file.abspath)
            basenode.files.remove(file)
            db.session.commit()
    return {'files': files, 'schemes': scheme_type}
예제 #10
0
파일: streams.py 프로젝트: hibozzy/mediatum
def send_attfile(req):
    access = AccessData(req)
    f = req.path[9:].split('/')
    try:
        node = getNode(f[0])
    except tree.NoSuchNodeError:
        return 404
    if not access.hasAccess(node, "data") and node.type != "directory":
        return 403
    if len([file for file in node.getFiles() if file._path in ["/".join(f[1:]), "/".join(f[1:-1])]]) == 0:  # check filepath
        return 403

    filename = clean_path("/".join(f[1:]))
    path = join_paths(config.get("paths.datadir"), filename)
    mime, type = getMimeType(filename)
    if(get_filesize(filename) > 16 * 1048576):
        req.reply_headers["Content-Disposition"] = 'attachment; filename="{}"'.format(filename)

    return req.sendFile(path, mime)
예제 #11
0
def serve_file(req, filepath):

    if 'mimetype' in req.params:
        mimetype = req.params.get('mimetype')
    elif filepath.lower().endswith('.html') or filepath.lower().endswith('.htm'):
        mimetype = 'text/html'
    else:
        mimetype = getMimeType(filepath)

    req.reply_headers['Content-Type'] = mimetype

    tmppath = config.get("paths.datadir") + "tmp/"

    abspath = os.path.join(tmppath, filepath)

    if os.path.isfile(abspath):
        filesize = os.path.getsize(abspath)
        req.sendFile(abspath, mimetype, force=1)
        return 200, filesize, abspath  # ok
    else:
        return 404, 0, abspath  # not found
예제 #12
0
파일: video.py 프로젝트: agromsl/mediatum
    def _prepareData(self, req, words=""):

        access = acl.AccessData(req)
        mask = self.getFullView(lang(req))

        obj = {'deleted': False, 'access': access}
        node = self

        if len(node.getFiles()) == 0:
            obj['captions_info'] = {}
            obj['file'] = ''
            obj['hasFiles'] = False
            obj['hasVidFiles'] = False

        if len(node.getFiles()) > 0:
            obj['hasFiles'] = True
            if len([m for m in [getMimeType(os.path.abspath(f.retrieveFile()))[0].split('/')[0] for f in self.getFiles() if os.path.exists(os.path.abspath(f.retrieveFile()))] if m == 'video'])>0:
                obj['hasVidFiles'] = True
            else:
                obj['hasVidFiles'] = False

        if self.get('deleted') == 'true':
            node = self.getActiveVersion()
            obj['deleted'] = True
        for filenode in node.getFiles():
            if filenode.getType() in ["original", "video"]:
                obj["file"] = "/file/%s/%s" % (node.id, filenode.getName())

        if mask:
            obj['metadata'] = mask.getViewHTML([node], VIEW_HIDE_EMPTY, lang(req), mask=mask)  # hide empty elements
        else:
            obj['metadata'] = []
        obj['node'] = node
        obj['path'] = req.params.get("path", "")
        obj['canseeoriginal'] = access.hasAccess(node, "data")
        obj['parentInformation'] = self.getParentInformation(req)

        return obj
예제 #13
0
def serve_file(req, path, params, data, filepath):
    atime = starttime = time.time()

    d = {}
    d['timetable'] = []

    if 'mimetype' in params:
        mimetype = params['mimetype']
    elif filepath.lower().endswith('.html') or filepath.lower().endswith(
            '.htm'):
        mimetype = 'text/html'
    else:
        mimetype = getMimeType(filepath)

    req.reply_headers['Content-Type'] = mimetype

    if WEBROOT:
        basedir = WEBROOT
    else:
        basedir = os.path.dirname(os.path.abspath(__file__))
    abspath = os.path.join(basedir, 'static', filepath)
    logg.info("web service trying to serve: %s", abspath)
    if os.path.isfile(abspath):
        filesize = os.path.getsize(abspath)
        _sendFile(req, abspath, mimetype, force=1)
        d['timetable'].append(
            ["reading file '%s'" % filepath,
             time.time() - atime])
        atime = time.time()
        d['status'] = 'ok'
        dataready = "%.3f" % (time.time() - starttime)
        d['dataready'] = dataready
        return 200, filesize, d  # ok
    else:
        d['status'] = 'fail'
        dataready = "%.3f" % (time.time() - starttime)
        d['dataready'] = dataready
        return 404, 0, d  # not found
예제 #14
0
def read_serve_file(req, filepath, remove_after_sending=False):

    if 'mimetype' in req.params:
        mimetype = req.params.get('mimetype')
    elif filepath.lower().endswith('.html') or filepath.lower().endswith('.htm'):
        mimetype = 'text/html'
    else:
        mimetype = getMimeType(filepath)

    req.reply_headers['Content-Type'] = mimetype

    abspath = filepath

    if os.path.isfile(abspath):

        f = open(abspath, "rb")
        s = f.read()
        f.close()
        if remove_after_sending:
            os.remove(abspath)
        req.write(s)
        return 200, len(s), abspath  # ok
    else:
        return 404, 0, abspath  # not found
예제 #15
0
def read_serve_file(req, filepath, remove_after_sending=False):

    if 'mimetype' in req.params:
        mimetype = req.params.get('mimetype')
    elif filepath.lower().endswith('.html') or filepath.lower().endswith('.htm'):
        mimetype = 'text/html'
    else:
        mimetype = getMimeType(filepath)

    req.reply_headers['Content-Type'] = mimetype

    abspath = filepath

    if os.path.isfile(abspath):

        f = open(abspath, "rb")
        s = f.read()
        f.close()
        if remove_after_sending:
            os.remove(abspath)
        req.write(s)
        return 200, len(s), abspath  # ok
    else:
        return 404, 0, abspath  # not found
예제 #16
0
def send_attfile(req):
    access = AccessData(req)
    f = req.path[9:].split('/')
    try:
        node = getNode(f[0])
    except tree.NoSuchNodeError:
        return 404
    if not access.hasAccess(node, "data") and node.type != "directory":
        return 403
    if len([
            file for file in node.getFiles()
            if file._path in ["/".join(f[1:]), "/".join(f[1:-1])]
    ]) == 0:  # check filepath
        return 403

    filename = clean_path("/".join(f[1:]))
    path = join_paths(config.get("paths.datadir"), filename)
    mime, type = getMimeType(filename)
    if (get_filesize(filename) > 16 * 1048576):
        req.reply_headers[
            "Content-Disposition"] = 'attachment; filename="{}"'.format(
                filename)

    return req.sendFile(path, mime)
예제 #17
0
파일: logo.py 프로젝트: mediatum/mediatum
def getContent(req, ids):
    user = current_user
    node = q(Node).get(ids[0])

    if "logo" in current_user.hidden_edit_functions or not node.has_write_access():
        req.setStatus(httpstatus.HTTP_FORBIDDEN)
        return req.getTAL("web/edit/edit.html", {}, macro="access_error")

    # delete logo file
    if "action" in req.params and req.params.get('action') == "delete":
        file = req.params.get('file').split("/")[-1]
        for f in node.files:
            if f.abspath.endswith(file):
                node.files.remove(f)
                db.session.commit()
                req.write('ok')
                return None
        req.write('not found')
        return None

    # add logo file
    if "addfile" in req.params.keys():
        file = req.params.get("updatefile")
        if file:
            mimetype = "application/x-download"
            type = "file"
            mimetype, type = getMimeType(file.filename.lower())

            if mimetype not in ("image/jpeg", "image/gif", "image/png"):
                # wrong file type (jpeg, jpg, gif, png)
                req.setStatus(httpstatus.HTTP_INTERNAL_SERVER_ERROR)
                return req.getTAL("web/edit/modules/logo.html", {}, macro="filetype_error")
            else:
                file = importFile(file.filename, file.tempname)
                node.files.append(file)
                db.session.commit()

    # save logo
    if "logo_save" in req.params.keys():
        # save url
        if req.params.get("logo_link", "") == "":
            if 'url' in node.attrs:
                del node.attrs['url']
        else:
            node.set('url', req.params.get("logo_link"))

        # save filename
        if req.params.get('logo') == "/img/empty.gif":
            # remove logo from current node
            node.set("system.logo", "")
            logg.info("%s cleared logo for node %s (%s, %s)", user.login_name, node.id, node.name, node.type)
        else:
            node.set("system.logo", req.params.get("logo").split("/")[-1])
            logg.info("%s set logo for node %s (%s, %s) to %s", user.login_name, node.id, node.name, node.type, node.get("system.logo"))

        db.session.commit()

    logofiles = []
    for f in node.files:
        if f.filetype == "image":
            logofiles.append(splitpath(f.abspath))
    
    v = {
        "id": req.params.get("id", "0"),
        "tab": req.params.get("tab", ""),
        "node": node,
        "logofiles": logofiles,
        "logo": node.getLogoPath(),
        "language": lang(req),
        "t": translation_t,
        "csrf": req.csrf_token.current_token
    }
         
    return req.getTAL("web/edit/modules/logo.html", v, macro="edit_logo")
예제 #18
0
파일: streams.py 프로젝트: hibozzy/mediatum
def get_root(req):
    filename = config.basedir + "/web/root" + req.path
    if os.path.isfile(filename):
        return req.sendFile(filename, getMimeType(filename)[0])
    else:
        return 404
예제 #19
0
def send_file(req):
    parts = splitpath(req.path)
    if len(parts) != 2:
        return 400

    nidstr, filename = parts
    if nidstr.endswith("_transfer.zip"):
        nidstr = nidstr[:-13]

    nid = userinput.string_to_int(nidstr)
    if nid is None:
        return 400

    version_id = version_id_from_req(req)

    node = get_node_or_version(nid, version_id)

    if (node is None
            or isinstance(node, Container) and not node.has_read_access()
            or isinstance(node, Content) and not node.has_data_access()):
        return 404

    def _send_attachment(filepath, mimetype):
        file_ext = os.path.splitext(filepath)[1]
        if existMetaField(node.schema, u'nodename'):
            display_file_name = u'{}{}'.format(
                os.path.splitext(os.path.basename(node.name))[0], file_ext)
        else:
            display_file_name = filename
        req.reply_headers[
            "Content-Disposition"] = u'attachment; filename="{}"'.format(
                display_file_name)
        return req.sendFile(filepath, mimetype)

    if filename is None:
        # build zip-file and return it
        with tempfile.NamedTemporaryFile() as tmpfile:
            files_written = build_transferzip(tmpfile, node)
            if files_written == 0:
                return 404
            return req.sendFile(tmpfile.name, "application/zip")

    # try full filename
    for f in node.files:
        if f.base_name == filename:
            return _send_attachment(f.abspath, f.mimetype)

    archive = get_archive_for_node(node)
    if archive:
        filepath = archive.get_local_filepath(node)
        mimetype, _ = getMimeType(filepath)
        return _send_attachment(filepath, mimetype)

    else:
        # try only extension
        file_ext = os.path.splitext(filename)[1]
        for f in node.files:
            if os.path.splitext(f.base_name)[1] == file_ext and f.filetype in [
                    u'document', u'original', u'mp3'
            ]:
                logg.warn(
                    "serving file %s for node %s only by matched extension",
                    f.path, node.id)
                return _send_attachment(f.abspath, f.mimetype)

    return 404
예제 #20
0
def getContent(req, ids):
    user = users.getUserFromRequest(req)
    node = tree.getNode(ids[0])
    access = acl.AccessData(req)

    if "logo" in users.getHideMenusForUser(user) or not access.hasWriteAccess(node):
        req.setStatus(httpstatus.HTTP_FORBIDDEN)
        return req.getTAL("web/edit/edit.html", {}, macro="access_error")

    # delete logo file
    if "action" in req.params and req.params.get('action') == "delete":
        file = req.params.get('file').split("/")[-1]
        for f in node.getFiles():
            if f.retrieveFile().endswith(file):
                node.removeFile(f)
                req.write('ok')
                return None
        req.write('not found')
        return None

    # add logo file
    if "addfile" in req.params.keys():
        file = req.params.get("updatefile")
        if file:
            mimetype = "application/x-download"
            type = "file"
            mimetype, type = getMimeType(file.filename.lower())

            if mimetype not in ("image/jpeg", "image/gif", "image/png"):
                # wrong file type (jpeg, jpg, gif, png)
                req.setStatus(httpstatus.HTTP_INTERNAL_SERVER_ERROR)
                return req.getTAL("web/edit/modules/logo.html", {}, macro="filetype_error")
            else:
                file = importFile(file.filename, file.tempname)
                node.addFile(file)

    # save logo
    if "logo_save" in req.params.keys():
        # save url
        if req.params.get("logo_link", "") == "":
            node.removeAttribute("url")
        else:
            node.set('url', req.params.get("logo_link"))

        # save filename
        if req.params.get('logo') == "/img/empty.gif":
            # remove logo from current node
            node.set("system.logo", "")
            msg = "%s cleared logo for node %r (%r, %r)" % (user.getName(), node.id, node.name, node.type)
            logger.info(msg)
            logger_e.info(msg)
        else:
            node.set("system.logo", req.params.get("logo").split("/")[-1])
            msg = "%s set logo for node %r (%r, %r) to %r" % (user.getName(), node.id, node.name, node.type, node.get("system.logo"))
            logger.info(msg)
            logger_e.info(msg)

    logofiles = []
    for f in node.getFiles():
        if f.getType() == "image":
            logofiles.append(splitpath(f.retrieveFile()))
    
    v = {
        "id": req.params.get("id", "0"),
        "tab": req.params.get("tab", ""),
        "node": node,
        "logofiles": logofiles,
        "logo": node.getLogoPath(),
        "language": lang(req),
        "t": translation_t
    }
         
    return req.getTAL("web/edit/modules/logo.html", v, macro="edit_logo")
예제 #21
0
def _finish_change(node, change_file, user, uploadfile, req):

    if change_file in ["yes", "no"]:

        # check that the correct filetype is uploaded
        # note: only the suffix of the filename is checked not the file content
        uploadfile_type = getMimeType(uploadfile.filename)[1]
        if uploadfile_type != node.type and uploadfile_type != node.get_upload_filetype(
        ):
            req.setStatus(httpstatus.HTTP_NOT_ACCEPTABLE)
            return

        # sys files are always cleared to delete remaining thumbnails, presentation images etc.
        for f in node.files:
            if f.filetype in node.get_sys_filetypes():
                node.files.remove(f)

        file = importFile(uploadfile.filename,
                          uploadfile.tempname)  # add new file
        file.filetype = node.get_upload_filetype()
        node.files.append(file)
        # this should re-create all dependent files
        node.event_files_changed()
        logg.info(u"%s changed file of node %s to %s (%s)", user.login_name,
                  node.id, uploadfile.filename, uploadfile.tempname)
        return

    attpath = ""
    for f in node.files:
        if f.mimetype == "inode/directory":
            attpath = f.base_name
            break

    if change_file == "attfile":  # add file as attachment
        if attpath == "":
            # no attachment directory existing
            file = importFileToRealname(uploadfile.filename,
                                        uploadfile.tempname)  # add new file
            file.mimetype = "inode/file"
            file.filetype = "attachment"
            node.files.append(file)
        else:
            # import attachment file into existing attachment directory
            importFileIntoDir(getImportDir() + "/" + attpath,
                              uploadfile.tempname)  # add new file

        # this should re-create all dependent files
        node.event_files_changed()
        logg.info(u"%s changed file of node %s to %s (%s)", user.login_name,
                  node.id, uploadfile.filename, uploadfile.tempname)

    if change_file == "addthumb":  # create new thumbanil from uploaded file
        thumbname = os.path.join(
            getImportDir(),
            hashlib.md5(ustr(random.random())).hexdigest()[0:8]) + ".thumb"

        file = importFile(thumbname, uploadfile.tempname)  # add new file
        make_thumbnail_image(file.abspath, thumbname)
        make_presentation_image(file.abspath, thumbname + "2")

        if os.path.exists(file.abspath):  # remove uploaded original
            os.remove(file.abspath)

        for f in node.files:
            if f.type in ["thumb", "presentation"]:
                if os.path.exists(f.abspath):
                    os.remove(f.abspath)
                node.files.remove(f)

        node.files.append(File(thumbname, "thumb", "image/jpeg"))
        node.files.append(File(thumbname + "2", "presentation", "image/jpeg"))
        logg.info("%s changed thumbnail of node %s", user.login_name, node.id)
        # this should re-create all dependent files
        node.event_files_changed()
        logg.info(u"%s changed file of node %s to %s (%s)", user.login_name,
                  node.id, uploadfile.filename, uploadfile.tempname)
예제 #22
0
파일: files.py 프로젝트: agromsl/mediatum
def getContent(req, ids):
    ret = ""
    user = users.getUserFromRequest(req)
    node = tree.getNode(ids[0])
    update_error = False
    access = acl.AccessData(req)

    msg = "%s|web.edit.modules.files.getContend|req.fullpath=%r|req.path=%r|req.params=%r|ids=%r" % (get_user_id(req), req.fullpath, req.path, req.params, ids)
    log.debug(msg)

    if not access.hasWriteAccess(node) or "files" in users.getHideMenusForUser(user):
        req.setStatus(httpstatus.HTTP_FORBIDDEN)
        return req.getTAL("web/edit/edit.html", {}, macro="access_error")

    if 'data' in req.params:
        if req.params.get('data') == 'children':  # get formated list of childnodes of selected directory
            excludeid = req.params.get('excludeid', None)
            if excludeid:
                grandchildren = []
                for child in node.getChildren():
                    for grandchild in child.getChildren():
                        if not grandchild.isContainer():
                            grandchildren.append(grandchild)
                req.writeTAL("web/edit/modules/files.html", {'children': [c for c in node.getChildren() if str(c.id) != excludeid],
                                                             'grandchildren': grandchildren}, macro="edit_files_popup_children")
            else:
                grandchildren = []
                for child in node.getChildren():
                    for grandchild in child.getChildren():
                        if not grandchild.isContainer():
                            grandchildren.append(grandchild)
                req.writeTAL("web/edit/modules/files.html", {'children': [c for c in node.getChildren() if str(c.id) != excludeid],
                                                             'grandchildren': grandchildren}, macro="edit_files_popup_children")
        elif req.params.get('data') =='grandchildren':
            grandchildren = []
            for child in node.getChildren():
                if not child.isContainer():
                    for grandchild in child.getChildren():
                        if not grandchild.isContainer():
                            if not grandchild.isContainer():
                                grandchildren.append(grandchild)

            if len(node.getChildren())==0:
                req.writeTAL("web/edit/modules/files.html", {'grandchildren': []}, macro="edit_files_popup_grandchildren")
            else:
                req.writeTAL("web/edit/modules/files.html", {'grandchildren': grandchildren}, macro="edit_files_popup_grandchildren")

        if req.params.get('data') == 'additems':  # add selected node as children
            for childid in req.params.get('items').split(";"):
                if childid.strip() != "":
                    childnode = tree.getNode(childid.strip())
                    for p in childnode.getParents():
                        if p.isContainer():
                            logging.getLogger('editor').info("Removed childnode: {} from node: {}. - caused by adding".format(childnode.id, node.id))
                            p.removeChild(childnode)
                    node.addChild(childnode)
            req.writeTAL("web/edit/modules/files.html", {'children': node.getChildren(), 'node': node}, macro="edit_files_children_list")

        if req.params.get('data') == 'removeitem':  # remove selected childnode node

            try:
                remnode = tree.getNode(req.params.get('remove'))
                if len(remnode.getParents()) == 1:
                    users.getUploadDir(user).addChild(remnode)
                node.removeChild(remnode)
            except: # node not found
                pass
            req.writeTAL("web/edit/modules/files.html", {'children': node.getChildren(), 'node': node}, macro="edit_files_children_list")

        if req.params.get('data') == 'reorder':
            i = 0
            for id in req.params.get('order').split(","):
                if id != "":
                    n = tree.getNode(id)
                    n.setOrderPos(i)
                    i += 1

        if req.params.get('data') == 'translate':
            req.writeTALstr('<tal:block i18n:translate="" tal:content="msgstr"/>', {'msgstr': req.params.get('msgstr')})
        return ""

    if req.params.get("style") == "popup":
        v = {"basedirs": [tree.getRoot('home'), tree.getRoot('collections')]}
        id = req.params.get("id", tree.getRoot().id)
        v["script"] = "var currentitem = '%s';\nvar currentfolder = '%s';\nvar node = %s;" %(id, req.params.get('parent'), id)
        v["idstr"] = ",".join(ids)
        v["node"] = node

        if node.type != 'directory':
            try:
                if v['current_id'] == None:
                    v['current_id'] = node.id
            except KeyError:
                pass

        req.writeTAL("web/edit/modules/files.html", v, macro="edit_files_popup_selection")
        return ""

    if "operation" in req.params:
        op = req.params.get("operation")
        if op == "delete":
            for key in req.params.keys():  # delete file
                if key.startswith("del|"):
                    filename = key[4:-2].split("|")
                    for file in node.getFiles():
                        if file.getName() == filename[1] and file.type == filename[0]:
                            # remove all files in directory
                            if file.getMimeType() == "inode/directory":
                                for root, dirs, files in os.walk(file.retrieveFile()):
                                    for name in files:
                                        try:
                                            os.remove(root + "/" + name)
                                        except:
                                            pass
                                    os.removedirs(file.retrieveFile()+"/")
                            if len([f for f in node.getFiles() if f.getName()==filename[1] and f.type==filename[0]]) > 1:
                                # remove single file from database if there are duplicates
                                node.removeFile(file, single=True)
                            else:
                                # remove single file
                                node.removeFile(file)
                                try:
                                    os.remove(file.retrieveFile())
                                except:
                                    pass
                            break
                    break
                elif key.startswith("delatt|"):
                    for file in node.getFiles():
                        if file.getMimeType() == "inode/directory":
                            try:
                                os.remove(file.retrieveFile() + "/" + key.split("|")[2][:-2])
                            except:
                                pass
                            break
                    break

        elif op=="change":
            uploadfile = req.params.get("updatefile")

            if uploadfile:
                create_version_error = False
                # Create new version when change file
                if (req.params.get('generate_new_version') and not hasattr(node, "metaFields")):
                    if (req.params.get('version_comment', '').strip()==''
                        or req.params.get('version_comment', '').strip()=='&nbsp;'):
                        create_version_error = True
                        req.setStatus(httpstatus.HTTP_INTERNAL_SERVER_ERROR)
                        ret += req.getTAL("web/edit/modules/files.html", {}, macro="version_error")
                    else:
                        current = node
                        node = node.createNewVersion(user)

                        for attr, value in current.items():
                            if node.get(attr)!="": # do not overwrite attributes
                                pass
                            else:
                                node.set(attr, value)
                        req.setStatus(httpstatus.HTTP_MOVED_TEMPORARILY)
                        ret += req.getTAL("web/edit/modules/metadata.html", {'url':'?id='+node.id+'&tab=files', 'pid':None}, macro="redirect")

                if req.params.get("change_file")=="yes" and not create_version_error: # remove old files
                    if uploadfile.filename:
                        if getMimeType(uploadfile.filename)[0] == 'other':
                            return '<h2 style="width:100%; text-align:center ;color:red">file-format is not supported (upload canceled / use attachment)</h2>'
                    for f in node.getFiles():
                        if f.getType() in node.getSysFiles():
                            node.removeFile(f)
                    node.set("system.version.comment", '('+t(req, "edit_files_new_version_exchanging_comment")+')\n'+req.params.get('version_comment', ''))

                if req.params.get("change_file")=="no" and not create_version_error:
                    node.set("system.version.comment", '('+t(req, "edit_files_new_version_adding_comment")+')\n'+req.params.get('version_comment', ''))

                if req.params.get("change_file") in ["yes", "no"] and not create_version_error:
                    if uploadfile.filename:
                        if getMimeType(uploadfile.filename)[0] == 'other':
                            return '<h2 style="width:100%; text-align:center ;color:red">file-format is not supported (upload canceled / use attachment)</h2>'
                    file = importFile(uploadfile.filename, uploadfile.tempname) # add new file
                    node.addFile(file)
                    logging.getLogger('usertracing').info(user.name+" changed file of node "+node.id+" to "+uploadfile.filename+" ("+uploadfile.tempname+")")

                attpath = ""
                for f in node.getFiles():
                    if f.getMimeType()=="inode/directory":
                        attpath = f.getName()
                        break

                if req.params.get("change_file")=="attdir" and not create_version_error: # add attachmentdir
                    dirname = req.params.get("inputname")

                    if attpath=="": # add attachment directory
                        attpath = req.params.get("inputname")
                        if not os.path.exists(getImportDir() + "/" + attpath):
                            os.mkdir(getImportDir() + "/" + attpath)
                            node.addFile(tree.FileNode(name=getImportDir() + "/" + attpath, mimetype="inode/directory", type="attachment"))

                        file = importFileIntoDir(getImportDir() + "/" + attpath, uploadfile.tempname) # add new file
                    node.set("system.version.comment", '('+t(req, "edit_files_new_version_attachment_directory_comment")+')\n'+req.params.get('version_comment', ''))
                    pass


                if req.params.get("change_file")=="attfile" and not create_version_error: # add file as attachment
                    if attpath=="":
                        # no attachment directory existing
                        file = importFile(uploadfile.filename, uploadfile.tempname) # add new file
                        file.mimetype = "inode/file"
                        file.type = "attachment"
                        node.addFile(file)
                    else:
                        # import attachment file into existing attachment directory
                        file = importFileIntoDir(getImportDir() + "/" + attpath, uploadfile.tempname) # add new file
                    node.set("system.version.comment", '('+t(req, "edit_files_new_version_attachment_comment")+')\n'+req.params.get('version_comment', ''))
                    pass

        elif op == "addthumb":  # create new thumbanil from uploaded file
            uploadfile = req.params.get("updatefile")

            if uploadfile:
                thumbname = os.path.join(getImportDir(), hashlib.md5(str(random.random())).hexdigest()[0:8]) + ".thumb"

                file = importFile(thumbname, uploadfile.tempname)  # add new file
                makeThumbNail(file.retrieveFile(), thumbname)
                makePresentationFormat(file.retrieveFile(), thumbname + "2")

                if os.path.exists(file.retrieveFile()):  # remove uploaded original
                    os.remove(file.retrieveFile())

                for f in node.getFiles():
                    if f.type in ["thumb", "presentation", "presentati"]:
                        if os.path.exists(f.retrieveFile()):
                            os.remove(f.retrieveFile())
                        node.removeFile(f)

                node.addFile(tree.FileNode(name=thumbname, type="thumb", mimetype="image/jpeg"))
                node.addFile(tree.FileNode(name=thumbname + "2", type="presentation", mimetype="image/jpeg"))
                logging.getLogger('usertracing').info(user.name + " changed thumbnail of node " + node.id)

        elif op == "postprocess":
            if hasattr(node, "event_files_changed"):
                try:
                    node.event_files_changed()
                    logging.getLogger('usertracing').info(user.name + " postprocesses node " + node.id)
                except:
                    update_error = True

    v = {"id": req.params.get("id", "0"), "tab": req.params.get("tab", ""), "node": node, "update_error": update_error,
         "user": user, "files": filter(lambda x: x.type != 'statistic', node.getFiles()),
         "statfiles": filter(lambda x: x.type == 'statistic', node.getFiles()),
         "attfiles": filter(lambda x: x.type == 'attachment', node.getFiles()), "att": [], "nodes": [node], "access": access}

    for f in v["attfiles"]:  # collect all files in attachment directory
        if f.getMimeType() == "inode/directory":
            for root, dirs, files in os.walk(f.retrieveFile()):
                for name in files:
                    af = tree.FileNode(root + "/" + name, "attachmentfile", getMimeType(name)[0])
                    v["att"].append(af)

    return req.getTAL("web/edit/modules/files.html", v, macro="edit_files_file")
예제 #23
0
def filebrowser(node, req):
    filesize = 0
    ret = list()
    if isinstance(node, Node):
        file_entity = File
    else:
        file_entity = version_class(File)

    paths = [
        t[0] for t in node.files.with_entities(file_entity.path).filter_by(
            filetype=u"attachment")
    ]

    if len(paths) == 1 and os.path.isdir(
            config.get("paths.datadir") + paths[0]):
        # single file with no path
        path = paths[0]
    elif len(paths) > 0:
        # some single files
        files = []
        for path in paths:
            file = {}
            if not os.path.isdir(config.get("paths.datadir") + path):  # file
                file["mimetype"], file["type"] = getMimeType(
                    config.get("paths.datadir") + path)
                icon = fileicons.get(file["mimetype"])
                if not icon:
                    icon = fileicons["other"]

                file["icon"] = icon
                file["path"] = path
                file["name"] = os.path.basename(path)
                if os.path.exists(config.get("paths.datadir") + path):
                    size = os.path.getsize(config.get("paths.datadir") + path)
                else:
                    size = 0
                file["size"] = format_filesize(size)
                filesize += int(size)
                files.append(file)

        return files, filesize
    else:
        path = ""

    if path == "":
        # no attachment directory -> test for single file

        for f in node.files.filter(
                ~file_entity.filetype.in_(node.get_sys_filetypes())):
            file = {}
            file["mimetype"], file["type"] = getMimeType(f.getName())
            file["icon"] = fileicons[file["mimetype"]]
            file["path"] = f.path
            file["name"] = f.base_name
            file["size"] = format_filesize(f.size)
            filesize += f.size
            ret.append(file)
        return ret, filesize

    if not path.endswith("/") and not req.params.get("path",
                                                     "").startswith("/"):
        path += "/"
    path += req.params.get("path", "")

    if req.params.get("path", "") != "":
        file = {}
        file["type"] = "back"
        file["mimetype"] = "back"
        file["icon"] = fileicons[file["mimetype"]]
        file["name"] = ".."
        file["path"] = req.params.get("path", "")
        file["req_path"] = req.params.get(
            "path", "")[:req.params.get("path", "").rfind("/")]
        ret.append(file)

    for name in os.listdir(config.settings["paths.datadir"] + path + "/"):

        if name.endswith(".thumb") or name.endswith(".thumb2"):
            continue
        file = {}

        file_path = os.path.join(config.settings["paths.datadir"] + path, name)
        if os.path.isdir(file_path):
            # directory
            file["type"] = "dir"
            file["mimetype"] = "directory"
        else:
            # file
            file["mimetype"], file["type"] = getMimeType(name)
            file["size"] = format_filesize(os.path.getsize(file_path))
            filesize += os.path.getsize(file_path)

        file["icon"] = fileicons[file["mimetype"]]
        file["path"] = os.path.join(path, name)
        file["name"] = name
        file["req_path"] = req.params.get("path", "") + "/" + file["name"]
        ret.append(file)

    return ret, format_filesize(filesize)
예제 #24
0
파일: upload.py 프로젝트: mediatum/mediatum
def getContent(req, ids):

    user = users.getUserFromRequest(req)
    language = lang(req)

    def get_ids_from_query():
        ids = get_ids_from_req(req)
        return ",".join(ids)

    if "action" in req.params:
        state = 'ok'

        if req.params.get('action') == "removefiles":
            basenode = q(Node).get(req.params.get('id'))
            for f in basenode.files:
                try:
                    os.remove(f.abspath)
                    pass
                except:
                    state = "error"
            basenode.files = []
            db.session.commit()
            req.write(json.dumps({'state': state}, ensure_ascii=False))
            return None

        if req.params.get('action') == "buildnode":  # create nodes
            basenode = q(Node).get(req.params.get('id'))
            newnodes = []
            errornodes = []
            basenodefiles_processed = []
            if req.params.get('uploader', '') == 'plupload':
                filename2scheme = {}
                for k in req.params:
                    if k.startswith("scheme_"):
                        filename2scheme[
                            k.replace('scheme_', '', 1)] = req.params.get(k)

                for f in basenode.files:
                    filename = f.name
                    if filename in filename2scheme:
                        mimetype = getMimeType(filename)

                        if mimetype[1] == "bibtex":  # bibtex import handler
                            try:
                                new_node = importBibTeX(f.abspath, basenode, req=req)
                                newnodes.append(new_node.id)
                                basenodefiles_processed.append(f)
                            except ValueError, e:
                                errornodes.append((filename, unicode(e)))

                        logg.debug("filename: %s, mimetype: %s", filename, mimetype)
                        logg.debug("__name__=%s, func=%s; _m=%s, _m[1]=%s", __name__, funcname(), mimetype, mimetype[1])

                        content_class = Node.get_class_for_typestring(mimetype[1])
                        node = content_class(name=filename, schema=filename2scheme[filename])

                        basenode.children.append(node)
                        node.set("creator", user.login_name)
                        node.set("creationtime",  unicode(time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime(time.time()))))
                        # set filetype for uploaded file as requested by the content class
                        f.filetype = content_class.get_upload_filetype()
                        node.files.append(f)
                        node.event_files_changed()
                        newnodes.append(node.id)
                        basenodefiles_processed.append(f)
                        basenode.files.remove(f)
                        db.session.commit()
                        logg.info("%s created new node id=%s (name=%s, type=%s) by uploading file %s, "
                            "node is child of base node id=%s (name=%s, type=%s)", user.login_name, node.id, node.name, node.type,
                             filename, basenode.id, basenode.name, basenode.type)

            else:
                for filename in req.params.get('files').split('|'):
                    mimetype = getMimeType(filename)
                    logg.debug("... in %s.%s: getMimeType(filename=%s)=%s", __name__, funcname(), filename, mimetype)
                    if mimetype[1] == req.params.get('type') or req.params.get('type') == 'file':
                        for f in basenode.files:
                            # ambiguity here ?
                            if f.abspath.endswith(filename):
                                # bibtex import handler
                                if mimetype[1] == "bibtex" and not req.params.get('type') == 'file':
                                    try:
                                        new_node = importBibTeX(f.abspath, basenode, req=req)
                                        newnodes.append(new_node.id)
                                        basenodefiles_processed.append(f)
                                    except ValueError, e:
                                        errornodes.append((filename, unicode(e)))
                                    db.session.commit()
                                else:

                                    logg.debug("creating new node: filename: %s", filename)
                                    logg.debug("files at basenode: %s", [(x.getName(), x.abspath) for x in basenode.files])

                                    content_class = Node.get_class_for_typestring(req.params.get('type'))
                                    node = content_class(name=filename, schema=req.params.get('value'))

                                    basenode.children.append(node)
                                    node.set("creator", user.login_name)
                                    node.set("creationtime",  unicode(time.strftime('%Y-%m-%dT%H:%M:%S',
                                                                                    time.localtime(time.time()))))

                                    # clones to a file with random name
                                    cloned_file = importFileRandom(f.abspath)
                                    # set filetype for uploaded file as requested by the content class
                                    cloned_file.filetype = content_class.get_upload_filetype()
                                    node.files.append(cloned_file)
                                    node.event_files_changed()
                                    newnodes.append(node.id)
                                    basenodefiles_processed.append(f)

                                    logg.info("%s created new node id=%s (name=%s, type=%s) by uploading file %s, "
                                    "node is child of base node id=%s (name=%s, type=%s)", user.login_name, node.id, node.name, node.type, filename,
                                    basenode.id, basenode.name, basenode.type)

                                    break  # filename may not be unique

            new_tree_labels = [{'id': basenode.id, 'label': getTreeLabel(basenode, lang=language)}]
            for f in basenodefiles_processed:
                basenode.files.remove(f)
                f_path = f.abspath
                if os.path.exists(f_path):
                    logg.debug("%s going to remove file %s from disk", user.login_name, f_path)
                    os.remove(f_path)

            mime = getMimeType(filename)
            scheme_type = {mime[1]: []}
            for scheme in get_permitted_schemas():
                if mime[1] in scheme.getDatatypes():
                    scheme_type[mime[1]].append(scheme)
                    # break

            db.session.commit()
            # standard file
            content = req.getTAL('web/edit/modules/upload.html', {'files': [filename], 'schemes': scheme_type}, macro="uploadfileok")

            res = {'state': state, 'newnodes': newnodes, 'errornodes':
                              errornodes, 'new_tree_labels': new_tree_labels, 'ret': content}
            res = json.dumps(res, ensure_ascii=False)
            req.write(res)
            return None
예제 #25
0
def sendBibFile(req, path):
    req.reply_headers['Content-Disposition'] = "attachment; filename=export.bib"
    _sendFile(req, path, getMimeType(path))
    if os.sep == '/':  # Unix?
        os.unlink(path)  # unlinking files while still reading them only works on Unix/Linux
예제 #26
0
파일: files.py 프로젝트: mediatum/mediatum
def getContent(req, ids):
    ret = ""
    user = current_user
    node = q(Node).get(ids[0])
    update_error = False
    update_error_extension = False

    logg.debug("%s|web.edit.modules.files.getContend|req.fullpath=%s|req.path=%s|req.params=%s|ids=%s",
               get_user_id(req), req.fullpath, req.path, req.params, ids)

    if not node.has_write_access() or "files" in current_user.hidden_edit_functions:
        req.setStatus(httpstatus.HTTP_FORBIDDEN)
        return req.getTAL("web/edit/edit.html", {}, macro="access_error")

    if 'data' in req.params:
        if 'data' in req.params:
            from contenttypes.container import Container
            if req.params.get('data') == 'children':  # get formated list of childnodes of selected directory
                excludeid = str(req.params.get('excludeid', None))
                if excludeid:
                    grandchildren = []

                    for child in node.getChildren():
                        for grandchild in child.children.all():
                            if not isinstance(grandchild, Container):
                                grandchildren.append(grandchild)
                    req.writeTAL("web/edit/modules/files.html", {'children': [c for c in node.children.all() if str(c.id) != excludeid],
                                                                 'grandchildren': grandchildren, "csrf": req.csrf_token.current_token}, macro="edit_files_popup_children")
                else:
                    grandchildren = []
                    for child in node.children.all():
                        for grandchild in child.children.all():
                            if not isinstance(grandchild, Container):
                                grandchildren.append(grandchild)
                    req.writeTAL("web/edit/modules/files.html", {'children': [c for c in node.getChildren() if str(c.id) != excludeid],
                                                                 'grandchildren': grandchildren, "csrf": req.csrf_token.current_token}, macro="edit_files_popup_children")
            elif req.params.get('data') =='grandchildren':
                grandchildren = []
                for child in node.children.all():
                    if not isinstance(child, Container):
                        for grandchild in child.children.all():
                            if not isinstance(grandchild, Container):
                                    grandchildren.append(grandchild)

                if len(node.getChildren())==0:
                    req.writeTAL("web/edit/modules/files.html", {'grandchildren': [], "csrf": req.csrf_token.current_token}, macro="edit_files_popup_grandchildren")
                else:
                    req.writeTAL("web/edit/modules/files.html", {'grandchildren': grandchildren, "csrf": req.csrf_token.current_token}, macro="edit_files_popup_grandchildren")

        if req.params.get('data') == 'additems':  # add selected node as children
            for childid in req.params.get('items').split(";"):
                if childid.strip() != "":
                    childnode = q(Node).get(childid.strip())
                    # don't try to add node as child to itself !
                    if childnode != node:
                        for p in childnode.parents:
                            if isinstance(p, Container):
                                p.children.remove(childnode)
                        node.children.append(childnode)
            req.writeTAL("web/edit/modules/files.html", {'children': node.children, 'node': node, "csrf": req.csrf_token.current_token}, macro="edit_files_children_list")

        if req.params.get('data') == 'removeitem':  # remove selected childnode node
            with suppress(Exception):
                remnode = q(Node).get(req.params.get('remove'))
                if len(remnode.parents) == 1:
                    users.getUploadDir(user).children.append(remnode)
                node.children.remove(remnode)

            req.writeTAL("web/edit/modules/files.html", {'children': node.children, 'node': node, "csrf": req.csrf_token.current_token}, macro="edit_files_children_list")

        if req.params.get('data') == 'reorder':
            i = 0
            for id in req.params.get('order').split(","):
                if id != "":
                    n = q(Node).get(id)
                    n.setOrderPos(i)
                    i += 1

        if req.params.get('data') == 'translate':
            req.writeTALstr('<tal:block i18n:translate="" tal:content="msgstr"/>', {'msgstr': req.params.get('msgstr'), "csrf": req.csrf_token.current_token})

        db.session.commit()
        return ""

    if req.params.get("style") == "popup":
        v = {"basedirs": [q(Home).one(), q(Collections).one()]}
        id = req.params.get("id", q(Root).one().id)
        v["script"] = "var currentitem = '%s';\nvar currentfolder = '%s';\nvar node = %s;" % (id, req.params.get('parent'), id)
        v["idstr"] = ",".join(ids)
        v["node"] = node
        v["csrf"] = req.csrf_token.current_token
        req.writeTAL("web/edit/modules/files.html", v, macro="edit_files_popup_selection")
        return ""

    if "operation" in req.params:
        op = req.params.get("operation")
        if op == "delete":
            for key in req.params.keys():  # delete file
                if key.startswith("del|"):
                    filename = key[4:-2].split("|")
                    for file in node.files:
                        if file.base_name == filename[1] and file.filetype == filename[0]:
                            # remove all files in directory
                            if file.mimetype == "inode/directory":
                                for root, dirs, files in os.walk(file.abspath):
                                    for name in files:
                                        try:
                                            os.remove(root + "/" + name)
                                        except:
                                            logg.exception("exception while removing file, ignore")
                                    os.removedirs(file.abspath + "/")
                            node.files.remove(file)
                            with suppress(Exception, warn=False):
                                os.remove(file.abspath)

                            break
                    break
                elif key.startswith("delatt|"):
                    for file in node.files:
                        if file.mimetype == "inode/directory":
                            try:
                                os.remove(file.abspath + "/" + key.split("|")[2][:-2])
                            except:
                                logg.exception("exception while removing file, ignore")
                            break
                    break

        elif op == "change":
            _handle_change(node, req)
            if req.reply_code != httpstatus.HTTP_OK and req.reply_code != httpstatus.HTTP_MOVED_TEMPORARILY:
                if req.reply_code is httpstatus.HTTP_NOT_ACCEPTABLE:
                    update_error_extension = True
                else:
                    update_error = True

        elif op == "addthumb":  # create new thumbanil from uploaded file
            uploadfile = req.params.get("updatefile")

            if uploadfile:
                thumbname = os.path.join(getImportDir(), hashlib.md5(ustr(random.random())).hexdigest()[0:8]) + ".thumb"

                file = importFile(thumbname, uploadfile.tempname)  # add new file
                make_thumbnail_image(file.abspath, thumbname)
                make_presentation_image(file.abspath, thumbname + "2")

                if os.path.exists(file.abspath):  # remove uploaded original
                    os.remove(file.abspath)

                for f in node.files:
                    if f.type in ["thumb", "presentation"]:
                        if os.path.exists(f.abspath):
                            os.remove(f.abspath)
                        node.files.remove(f)

                node.files.append(File(thumbname, "thumb", "image/jpeg"))
                node.files.append(File(thumbname + "2", "presentation", "image/jpeg"))
                logg.info("%s changed thumbnail of node %s", user.login_name, node.id)

        elif op == "postprocess":
                try:
                    node.event_files_changed()
                    logg.info("%s postprocesses node %s", user.login_name, node.id)
                except:
                    update_error = True

    db.session.commit()

    v = {"id": req.params.get("id", "0"),
         "tab": req.params.get("tab", ""),
         "node": node,
         "update_error": update_error,
         "update_error_extension": update_error_extension,
         "user": user,
         "files": filter(lambda x: x.type != 'statistic', node.files),
         "statfiles": filter(lambda x: x.type == 'statistic', node.files),
         "attfiles": filter(lambda x: x.type == 'attachment', node.files),
         "att": [],
         "nodes": [node],
         "csrf": req.csrf_token.current_token
        }

    for f in v["attfiles"]:  # collect all files in attachment directory
        if f.mimetype == "inode/directory":
            for root, dirs, files in os.walk(f.abspath):
                for name in files:
                    af = File(root + "/" + name, "attachmentfile", getMimeType(name)[0])
                    v["att"].append(af)

    return req.getTAL("web/edit/modules/files.html", v, macro="edit_files_file")
예제 #27
0
파일: upload.py 프로젝트: hibozzy/mediatum
            #f = importFile(realname,tempname)
            n = tree.getNode(req.params.get('id'))
            n.addFile(f)
            req.write("")
            logger.debug("%r|%s.%s: added file to node %r (%r, %r)" % (get_user_id(req), __name__, funcname(), n.id, n.name, n.type))
            if not proceed_to_uploadcomplete:
                return None

        # upload done -> deliver view of object
        if proceed_to_uploadcomplete or req.params.get('action') == "uploadcomplete":
            logger.debug("upload done -> deliver view of object")

            if proceed_to_uploadcomplete:
                req.params['file'] = realname

            mime = getMimeType(req.params.get('file'))
            data_extra = req.params.get('data_extra', '')
            if data_extra == 'tofile':

                ctx = {
                    'filename': f.getName(),
                }
                ctx.update(upload_to_filetype_filehandler(req))
                content = req.getTAL('web/edit/modules/upload.html', ctx, macro="uploadfileok_plupload")
                basenode = tree.getNode(req.params.get('id'))
                new_tree_labels = [{'id': basenode.id, 'label': getTreeLabel(basenode, lang=language)}]
                req.write(json.dumps({'type': 'file', 'ret': content, 'state': state, 'filename': req.params.get('file'), 'new_tree_labels': new_tree_labels}))
                return None

            if mime[1] == "other":  # file type not supported
                req.write(json.dumps({'type': mime[1], 'ret': req.getTAL('web/edit/modules/upload.html', {}, macro="uploadfileerror"), 'state': 'error', 'filename': req.params.get('file')}))
예제 #28
0
파일: upload.py 프로젝트: hibozzy/mediatum
def getContent(req, ids):

    user = users.getUserFromRequest(req)
    access = AccessData(user=user)
    language = lang(req)

    if "action" in req.params:
        state = 'ok'

        if req.params.get('action') == "removefiles":
            basenode = tree.getNode(req.params.get('id'))
            for f in basenode.getFiles():
                try:
                    os.remove(f.retrieveFile())
                    pass
                except:
                    state = "error"
            for f in basenode.getFiles():
                basenode.removeFile(f)
            req.write(json.dumps({'state': state}))
            return None

        if req.params.get('action') == "buildnode":  # create nodes
            basenode = tree.getNode(req.params.get('id'))
            newnodes = []
            errornodes = []
            basenodefiles_processed = []
            if req.params.get('uploader', '') == 'plupload':
                filename2scheme = {}
                for k in req.params:
                    if k.startswith("scheme_"):
                        filename2scheme[
                            k.replace('scheme_', '', 1)] = req.params.get(k)

                for f in basenode.getFiles():
                    filename = f.getName()
                    if filename in filename2scheme:
                        _m = getMimeType(filename)

                        if _m[1] == "bibtex":  # bibtex import handler
                            try:
                                nn = importBibTeX(f.retrieveFile(), basenode)
                                newnodes.append(nn.id)
                                basenodefiles_processed.append(f)
                            except ValueError, e:
                                errornodes.append((filename, str(e)))

                        logger.debug("filename: %r, mimetype: %r" % (filename, _m))
                        logger.debug("__name__=%r, func=%r; _m=%r, _m[1]=%r" % (__name__, funcname(), _m, _m[1]))

                        node_type = '%s/%s' % (_m[1], filename2scheme[filename])

                        n = tree.Node(filename, type=node_type)

                        basenode.addChild(n)
                        n.set("creator", user.getName())
                        n.set("creationtime",  str(time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime(time.time()))))

                        n.addFile(f)

                        n.event_files_changed()
                        clearFromCache(n)
                        newnodes.append(n.id)
                        basenodefiles_processed.append(f)
                        basenode.removeFile(f)
                        msg_t = (user.getName(), n.id, n.name, n.type,filename, basenode.id, basenode.name, basenode.type)
                        msg = "%s created new node id=%r (name=%r, type=%r) by uploading file %r, node is child of base node id=%r (name=%r, type=%r)" % msg_t
                        logger.info(msg)
                        logging.getLogger('usertracing').info(msg)

            else:
                for filename in req.params.get('files').split('|'):
                    _m = getMimeType(filename)
                    logger.debug("... in %s.%s: getMimeType(filename=%r)=%r" % (__name__, funcname(), filename, _m))
                    fs = basenode.getFiles()
                    logger.debug("... in %s.%s: basenode.id=%r, basenode_files: %r" % (__name__, funcname(), basenode.id, [(x.getName(), x.retrieveFile()) for x in fs]))
                    if _m[1] == req.params.get('type') or req.params.get('type') == 'file':
                        for f in basenode.getFiles():
                            # ambiguity here ?
                            if f.retrieveFile().endswith(filename):
                                # bibtex import handler
                                if _m[1] == "bibtex" and not req.params.get('type') == 'file':
                                    try:
                                        nn = importBibTeX(f.retrieveFile(), basenode)
                                        newnodes.append(nn.id)
                                        basenodefiles_processed.append(f)
                                    except ValueError, e:
                                        errornodes.append((filename, str(e)))
                                else:

                                    logger.debug("creating new node: filename: %r" % filename)
                                    logger.debug("files at basenode: %r" % [(x.getName(), x.retrieveFile()) for x in basenode.getFiles()])

                                    n = tree.Node(filename, type='%s/%s' % (req.params.get('type'), req.params.get('value')))
                                    basenode.addChild(n)
                                    n.set("creator", user.getName())
                                    n.set("creationtime",  str(time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime(time.time()))))
                                    # clones to a file with random name
                                    cloned_file = f.clone(None)
                                    n.addFile(cloned_file)
                                    if hasattr(n, 'event_files_changed'):
                                        n.event_files_changed()
                                    clearFromCache(n)
                                    newnodes.append(n.id)
                                    basenodefiles_processed.append(f)
                                    msg_t = (user.getName(), n.id, n.name, n.type, filename, basenode.id, basenode.name, basenode.type)
                                    msg = "%s created new node id=%r (name=%r, type=%r) by uploading file %r, node is child of base node id=%r (name=%r, type=%r)" % msg_t
                                    logger.info(msg)
                                    logging.getLogger('usertracing').info(msg)
                                    break  # filename may not be unique

            new_tree_labels = [{'id': basenode.id, 'label': getTreeLabel(basenode, lang=language)}]
            for f in basenodefiles_processed:
                basenode.removeFile(f)
                f_path = f.retrieveFile()
                if os.path.exists(f_path):
                    logger.debug("%s going to remove file %r from disk" % (user.getName(), f_path))
                    os.remove(f_path)

            mime = getMimeType(filename)
            scheme_type = {mime[1]: []}
            for scheme in getSchemes(req):
                if mime[1] in scheme.getDatatypes():
                    scheme_type[mime[1]].append(scheme)
                    # break

            # standard file
            content = req.getTAL('web/edit/modules/upload.html', {'files': [filename], 'schemes': scheme_type}, macro="uploadfileok")

            res = json.dumps({'state': state, 'newnodes': newnodes, 'errornodes':
                              errornodes, 'new_tree_labels': new_tree_labels, 'ret': content})
            req.write(res)
            return None
예제 #29
0
def getContent(req, ids):
    ret = ""
    user = current_user
    node = q(Node).get(ids[0])
    update_error = False
    update_error_extension = False

    logg.debug(
        "%s|web.edit.modules.files.getContend|req.fullpath=%s|req.path=%s|req.params=%s|ids=%s",
        get_user_id(req), req.fullpath, req.path, req.params, ids)

    if not node.has_write_access(
    ) or "files" in current_user.hidden_edit_functions:
        req.setStatus(httpstatus.HTTP_FORBIDDEN)
        return req.getTAL("web/edit/edit.html", {}, macro="access_error")

    if 'data' in req.params:
        if 'data' in req.params:
            from contenttypes.container import Container
            if req.params.get(
                    'data'
            ) == 'children':  # get formated list of childnodes of selected directory
                excludeid = str(req.params.get('excludeid', None))
                if excludeid:
                    grandchildren = []

                    for child in node.getChildren():
                        for grandchild in child.children.all():
                            if not isinstance(grandchild, Container):
                                grandchildren.append(grandchild)
                    req.writeTAL("web/edit/modules/files.html", {
                        'children': [
                            c for c in node.children.all()
                            if str(c.id) != excludeid
                        ],
                        'grandchildren':
                        grandchildren
                    },
                                 macro="edit_files_popup_children")
                else:
                    grandchildren = []
                    for child in node.children.all():
                        for grandchild in child.children.all():
                            if not isinstance(grandchild, Container):
                                grandchildren.append(grandchild)
                    req.writeTAL("web/edit/modules/files.html", {
                        'children': [
                            c for c in node.getChildren()
                            if str(c.id) != excludeid
                        ],
                        'grandchildren':
                        grandchildren
                    },
                                 macro="edit_files_popup_children")
            elif req.params.get('data') == 'grandchildren':
                grandchildren = []
                for child in node.children.all():
                    if not isinstance(child, Container):
                        for grandchild in child.children.all():
                            if not isinstance(grandchild, Container):
                                grandchildren.append(grandchild)

                if len(node.getChildren()) == 0:
                    req.writeTAL("web/edit/modules/files.html",
                                 {'grandchildren': []},
                                 macro="edit_files_popup_grandchildren")
                else:
                    req.writeTAL("web/edit/modules/files.html",
                                 {'grandchildren': grandchildren},
                                 macro="edit_files_popup_grandchildren")

        if req.params.get(
                'data') == 'additems':  # add selected node as children
            for childid in req.params.get('items').split(";"):
                if childid.strip() != "":
                    childnode = q(Node).get(childid.strip())
                    # don't try to add node as child to itself !
                    if childnode != node:
                        for p in childnode.parents:
                            if isinstance(p, Container):
                                p.children.remove(childnode)
                        node.children.append(childnode)
            req.writeTAL("web/edit/modules/files.html", {
                'children': node.children,
                'node': node
            },
                         macro="edit_files_children_list")

        if req.params.get(
                'data') == 'removeitem':  # remove selected childnode node
            try:
                remnode = q(Node).get(req.params.get('remove'))
                if len(remnode.parents) == 1:
                    users.getUploadDir(user).children.append(remnode)
                node.children.remove(remnode)
            except:  # node not found
                logg.exception(
                    "exception in getContent, node not found? ignore")
                pass

            req.writeTAL("web/edit/modules/files.html", {
                'children': node.children,
                'node': node
            },
                         macro="edit_files_children_list")

        if req.params.get('data') == 'reorder':
            i = 0
            for id in req.params.get('order').split(","):
                if id != "":
                    n = q(Node).get(id)
                    n.setOrderPos(i)
                    i += 1

        if req.params.get('data') == 'translate':
            req.writeTALstr(
                '<tal:block i18n:translate="" tal:content="msgstr"/>',
                {'msgstr': req.params.get('msgstr')})

        db.session.commit()
        return ""

    if req.params.get("style") == "popup":
        v = {"basedirs": [q(Home).one(), q(Collections).one()]}
        id = req.params.get("id", q(Root).one().id)
        v["script"] = "var currentitem = '%s';\nvar currentfolder = '%s';\nvar node = %s;" % (
            id, req.params.get('parent'), id)
        v["idstr"] = ",".join(ids)
        v["node"] = node
        req.writeTAL("web/edit/modules/files.html",
                     v,
                     macro="edit_files_popup_selection")
        return ""

    if "operation" in req.params:
        op = req.params.get("operation")
        if op == "delete":
            for key in req.params.keys():  # delete file
                if key.startswith("del|"):
                    filename = key[4:-2].split("|")
                    for file in node.files:
                        if file.base_name == filename[
                                1] and file.filetype == filename[0]:
                            # remove all files in directory
                            if file.mimetype == "inode/directory":
                                for root, dirs, files in os.walk(file.abspath):
                                    for name in files:
                                        try:
                                            os.remove(root + "/" + name)
                                        except:
                                            logg.exception(
                                                "exception while removing file, ignore"
                                            )
                                    os.removedirs(file.abspath + "/")
                            node.files.remove(file)
                            try:
                                os.remove(file.abspath)
                            except:
                                pass

                            break
                    break
                elif key.startswith("delatt|"):
                    for file in node.files:
                        if file.mimetype == "inode/directory":
                            try:
                                os.remove(file.abspath + "/" +
                                          key.split("|")[2][:-2])
                            except:
                                logg.exception(
                                    "exception while removing file, ignore")
                            break
                    break

        elif op == "change":
            _handle_change(node, req)
            if req.reply_code != httpstatus.HTTP_OK and req.reply_code != httpstatus.HTTP_MOVED_TEMPORARILY:
                if req.reply_code is httpstatus.HTTP_NOT_ACCEPTABLE:
                    update_error_extension = True
                else:
                    update_error = True

        elif op == "addthumb":  # create new thumbanil from uploaded file
            uploadfile = req.params.get("updatefile")

            if uploadfile:
                thumbname = os.path.join(
                    getImportDir(),
                    hashlib.md5(ustr(
                        random.random())).hexdigest()[0:8]) + ".thumb"

                file = importFile(thumbname,
                                  uploadfile.tempname)  # add new file
                make_thumbnail_image(file.abspath, thumbname)
                make_presentation_image(file.abspath, thumbname + "2")

                if os.path.exists(file.abspath):  # remove uploaded original
                    os.remove(file.abspath)

                for f in node.files:
                    if f.type in ["thumb", "presentation"]:
                        if os.path.exists(f.abspath):
                            os.remove(f.abspath)
                        node.files.remove(f)

                node.files.append(File(thumbname, "thumb", "image/jpeg"))
                node.files.append(
                    File(thumbname + "2", "presentation", "image/jpeg"))
                logg.info("%s changed thumbnail of node %s", user.login_name,
                          node.id)

        elif op == "postprocess":
            try:
                node.event_files_changed()
                logg.info("%s postprocesses node %s", user.login_name, node.id)
            except:
                update_error = True

    db.session.commit()

    v = {
        "id": req.params.get("id", "0"),
        "tab": req.params.get("tab", ""),
        "node": node,
        "update_error": update_error,
        "update_error_extension": update_error_extension,
        "user": user,
        "files": filter(lambda x: x.type != 'statistic', node.files),
        "statfiles": filter(lambda x: x.type == 'statistic', node.files),
        "attfiles": filter(lambda x: x.type == 'attachment', node.files),
        "att": [],
        "nodes": [node],
    }

    for f in v["attfiles"]:  # collect all files in attachment directory
        if f.mimetype == "inode/directory":
            for root, dirs, files in os.walk(f.abspath):
                for name in files:
                    af = File(root + "/" + name, "attachmentfile",
                              getMimeType(name)[0])
                    v["att"].append(af)

    return req.getTAL("web/edit/modules/files.html",
                      v,
                      macro="edit_files_file")
예제 #30
0
def getContent(req, ids):
    ret = ""
    user = users.getUserFromRequest(req)
    node = tree.getNode(ids[0])
    update_error = False
    access = acl.AccessData(req)

    msg = "%s|web.edit.modules.files.getContend|req.fullpath=%r|req.path=%r|req.params=%r|ids=%r" % (
        get_user_id(req), req.fullpath, req.path, req.params, ids)
    log.debug(msg)

    if not access.hasWriteAccess(node) or "files" in users.getHideMenusForUser(
            user):
        req.setStatus(httpstatus.HTTP_FORBIDDEN)
        return req.getTAL("web/edit/edit.html", {}, macro="access_error")

    if 'data' in req.params:
        if req.params.get(
                'data'
        ) == 'children':  # get formated list of childnodes of selected directory
            req.writeTAL("web/edit/modules/files.html",
                         {'children': node.getChildren()},
                         macro="edit_files_popup_children")

        if req.params.get(
                'data') == 'additems':  # add selected node as children
            for childid in req.params.get('items').split(";"):
                if childid.strip() != "":
                    childnode = tree.getNode(childid.strip())
                    for p in childnode.getParents():
                        p.removeChild(childnode)
                    node.addChild(childnode)
            req.writeTAL("web/edit/modules/files.html", {
                'children': node.getChildren(),
                'node': node
            },
                         macro="edit_files_children_list")

        if req.params.get(
                'data') == 'removeitem':  # remove selected childnode node
            try:
                remnode = tree.getNode(req.params.get('remove'))
                if len(remnode.getParents()) == 1:
                    users.getUploadDir(user).addChild(remnode)
                node.removeChild(remnode)
            except:  # node not found
                pass
            req.writeTAL("web/edit/modules/files.html", {
                'children': node.getChildren(),
                'node': node
            },
                         macro="edit_files_children_list")

        if req.params.get('data') == 'reorder':
            i = 0
            for id in req.params.get('order').split(","):
                if id != "":
                    n = tree.getNode(id)
                    n.setOrderPos(i)
                    i += 1

        if req.params.get('data') == 'translate':
            req.writeTALstr(
                '<tal:block i18n:translate="" tal:content="msgstr"/>',
                {'msgstr': req.params.get('msgstr')})
        return ""

    if req.params.get("style") == "popup":
        v = {"basedirs": [tree.getRoot('home'), tree.getRoot('collections')]}
        id = req.params.get("id", tree.getRoot().id)
        v["script"] = "var currentitem = '%s';\nvar currentfolder = '%s';\nvar node = %s;" % (
            id, req.params.get('parent'), id)
        v["idstr"] = ",".join(ids)
        v["node"] = node
        req.writeTAL("web/edit/modules/files.html",
                     v,
                     macro="edit_files_popup_selection")
        return ""

    if "operation" in req.params:
        op = req.params.get("operation")
        if op == "delete":
            for key in req.params.keys():  # delete file
                if key.startswith("del|"):
                    filename = key[4:-2].split("|")
                    for file in node.getFiles():
                        if file.getName(
                        ) == filename[1] and file.type == filename[0]:
                            # remove all files in directory
                            if file.getMimeType() == "inode/directory":
                                for root, dirs, files in os.walk(
                                        file.retrieveFile()):
                                    for name in files:
                                        try:
                                            os.remove(root + "/" + name)
                                        except:
                                            pass
                                    os.removedirs(file.retrieveFile() + "/")
                            if len([
                                    f for f in node.getFiles() if f.getName()
                                    == filename[1] and f.type == filename[0]
                            ]) > 1:
                                # remove single file from database if there are duplicates
                                node.removeFile(file, single=True)
                            else:
                                # remove single file
                                node.removeFile(file)
                                try:
                                    os.remove(file.retrieveFile())
                                except:
                                    pass
                            break
                    break
                elif key.startswith("delatt|"):
                    for file in node.getFiles():
                        if file.getMimeType() == "inode/directory":
                            try:
                                os.remove(file.retrieveFile() + "/" +
                                          key.split("|")[2][:-2])
                            except:
                                pass
                            break
                    break

        elif op == "change":
            uploadfile = req.params.get("updatefile")

            if uploadfile:
                create_version_error = False
                # Create new version when change file
                if (req.params.get('generate_new_version')
                        and not hasattr(node, "metaFields")):
                    if (req.params.get('version_comment', '').strip() == ''
                            or req.params.get('version_comment',
                                              '').strip() == '&nbsp;'):
                        create_version_error = True
                        req.setStatus(httpstatus.HTTP_INTERNAL_SERVER_ERROR)
                        ret += req.getTAL("web/edit/modules/files.html", {},
                                          macro="version_error")
                    else:
                        current = node
                        node = node.createNewVersion(user)

                        for attr, value in current.items():
                            if node.get(
                                    attr) != "":  # do not overwrite attributes
                                pass
                            else:
                                node.set(attr, value)
                        req.setStatus(httpstatus.HTTP_MOVED_TEMPORARILY)
                        ret += req.getTAL("web/edit/modules/metadata.html", {
                            'url': '?id=' + node.id + '&tab=files',
                            'pid': None
                        },
                                          macro="redirect")

                if req.params.get(
                        "change_file"
                ) == "yes" and not create_version_error:  # remove old files
                    for f in node.getFiles():
                        if f.getType() in node.getSysFiles():
                            node.removeFile(f)
                    node.set(
                        "system.version.comment", '(' +
                        t(req, "edit_files_new_version_exchanging_comment") +
                        ')\n' + req.params.get('version_comment', ''))

                if req.params.get(
                        "change_file") == "no" and not create_version_error:
                    node.set(
                        "system.version.comment",
                        '(' + t(req, "edit_files_new_version_adding_comment") +
                        ')\n' + req.params.get('version_comment', ''))

                if req.params.get("change_file") in [
                        "yes", "no"
                ] and not create_version_error:
                    file = importFile(uploadfile.filename,
                                      uploadfile.tempname)  # add new file
                    node.addFile(file)
                    logging.getLogger('usertracing').info(
                        user.name + " changed file of node " + node.id +
                        " to " + uploadfile.filename + " (" +
                        uploadfile.tempname + ")")

                attpath = ""
                for f in node.getFiles():
                    if f.getMimeType() == "inode/directory":
                        attpath = f.getName()
                        break

                if req.params.get(
                        "change_file"
                ) == "attdir" and not create_version_error:  # add attachmentdir
                    dirname = req.params.get("inputname")

                    if attpath == "":  # add attachment directory
                        attpath = req.params.get("inputname")
                        if not os.path.exists(getImportDir() + "/" + attpath):
                            os.mkdir(getImportDir() + "/" + attpath)
                            node.addFile(
                                tree.FileNode(name=getImportDir() + "/" +
                                              attpath,
                                              mimetype="inode/directory",
                                              type="attachment"))

                        file = importFileIntoDir(
                            getImportDir() + "/" + attpath,
                            uploadfile.tempname)  # add new file
                    node.set(
                        "system.version.comment", '(' + t(
                            req,
                            "edit_files_new_version_attachment_directory_comment"
                        ) + ')\n' + req.params.get('version_comment', ''))
                    pass

                if req.params.get(
                        "change_file"
                ) == "attfile" and not create_version_error:  # add file as attachment
                    if attpath == "":
                        # no attachment directory existing
                        file = importFile(uploadfile.filename,
                                          uploadfile.tempname)  # add new file
                        file.mimetype = "inode/file"
                        file.type = "attachment"
                        node.addFile(file)
                    else:
                        # import attachment file into existing attachment directory
                        file = importFileIntoDir(
                            getImportDir() + "/" + attpath,
                            uploadfile.tempname)  # add new file
                    node.set(
                        "system.version.comment", '(' +
                        t(req, "edit_files_new_version_attachment_comment") +
                        ')\n' + req.params.get('version_comment', ''))
                    pass

        elif op == "addthumb":  # create new thumbanil from uploaded file
            uploadfile = req.params.get("updatefile")

            if uploadfile:
                thumbname = os.path.join(
                    getImportDir(),
                    hashlib.md5(str(
                        random.random())).hexdigest()[0:8]) + ".thumb"

                file = importFile(thumbname,
                                  uploadfile.tempname)  # add new file
                makeThumbNail(file.retrieveFile(), thumbname)
                makePresentationFormat(file.retrieveFile(), thumbname + "2")

                if os.path.exists(
                        file.retrieveFile()):  # remove uploaded original
                    os.remove(file.retrieveFile())

                for f in node.getFiles():
                    if f.type in ["thumb", "presentation", "presentati"]:
                        if os.path.exists(f.retrieveFile()):
                            os.remove(f.retrieveFile())
                        node.removeFile(f)

                node.addFile(
                    tree.FileNode(name=thumbname,
                                  type="thumb",
                                  mimetype="image/jpeg"))
                node.addFile(
                    tree.FileNode(name=thumbname + "2",
                                  type="presentation",
                                  mimetype="image/jpeg"))
                logging.getLogger('usertracing').info(
                    user.name + " changed thumbnail of node " + node.id)

        elif op == "postprocess":
            if hasattr(node, "event_files_changed"):
                try:
                    node.event_files_changed()
                    logging.getLogger('usertracing').info(
                        user.name + " postprocesses node " + node.id)
                except:
                    update_error = True

    v = {
        "id": req.params.get("id", "0"),
        "tab": req.params.get("tab", ""),
        "node": node,
        "update_error": update_error,
        "user": user,
        "files": filter(lambda x: x.type != 'statistic', node.getFiles()),
        "statfiles": filter(lambda x: x.type == 'statistic', node.getFiles()),
        "attfiles": filter(lambda x: x.type == 'attachment', node.getFiles()),
        "att": [],
        "nodes": [node],
        "access": access
    }

    for f in v["attfiles"]:  # collect all files in attachment directory
        if f.getMimeType() == "inode/directory":
            for root, dirs, files in os.walk(f.retrieveFile()):
                for name in files:
                    af = tree.FileNode(root + "/" + name, "attachmentfile",
                                       getMimeType(name)[0])
                    v["att"].append(af)

    return req.getTAL("web/edit/modules/files.html",
                      v,
                      macro="edit_files_file")
예제 #31
0
def get_root(req):
    filename = config.basedir + "/web/root" + req.path
    if os.path.isfile(filename):
        return req.sendFile(filename, getMimeType(filename)[0])
    else:
        return 404
예제 #32
0
def filebrowser(node, req):
    filesize = 0
    ret = list()
    paths = []
    for f in node.getFiles():
        if f.getType() == "attachment":
            paths.append(f._path)
            # break

    if len(paths) == 1 and os.path.isdir(config.get("paths.datadir") + paths[0]):
        # single file with no path
        path = paths[0]
    elif len(paths) > 0:
        # some single files
        files = []
        for path in paths:
            file = {}
            if not os.path.isdir(config.get("paths.datadir") + path):  # file
                file["mimetype"], file["type"] = getMimeType(config.get("paths.datadir") + path)
                file["icon"] = fileicons[file["mimetype"]]
                file["path"] = path
                file["name"] = f.getName()
                if os.path.exists(config.get("paths.datadir") + path):
                    size = os.path.getsize(config.get("paths.datadir") + path)
                else:
                    size = 0
                file["size"] = format_filesize(size)
                filesize += int(size)
                files.append(file)

        return files, filesize
    else:
        path = ""

    if path == "":
        # no attachment directory -> test for single file
        file = {}
        for f in node.getFiles():
            if f.getType() not in node.getSysFiles():
                file["mimetype"], file["type"] = getMimeType(f.getName())
                file["icon"] = fileicons[file["mimetype"]]
                file["path"] = f._path
                file["name"] = f.getName()
                size = f.getSize() or 0
                file["size"] = format_filesize(size)
                filesize += f.getSize()
                ret.append(file)
        return ret, filesize

    if not path.endswith("/") and not req.params.get("path", "").startswith("/"):
        path += "/"
    path += req.params.get("path", "")

    if req.params.get("path", "") != "":
        file = {}
        file["type"] = "back"
        file["mimetype"] = "back"
        file["icon"] = fileicons[file["mimetype"]]
        file["name"] = ".."
        file["path"] = req.params.get("path", "")
        file["req_path"] = req.params.get("path", "")[:req.params.get("path", "").rfind("/")]
        ret.append(file)

    for name in os.listdir(config.settings["paths.datadir"] + path + "/"):

        if name.endswith(".thumb") or name.endswith(".thumb2"):
            continue
        file = {}

        file_path = os.path.join(config.settings["paths.datadir"] + path, name)
        if os.path.isdir(file_path):
            # directory
            file["type"] = "dir"
            file["mimetype"] = "directory"
        else:
            # file
            file["mimetype"], file["type"] = getMimeType(name)
            file["size"] = format_filesize(os.path.getsize(file_path))
            filesize += os.path.getsize(file_path)

        file["icon"] = fileicons[file["mimetype"]]
        file["path"] = os.path.join(path, name)
        file["name"] = name
        file["req_path"] = req.params.get("path", "") + "/" + file["name"]
        ret.append(file)

    return ret, format_filesize(filesize)
예제 #33
0
파일: streams.py 프로젝트: hibozzy/mediatum
def sendBibFile(req, path):
    req.reply_headers['Content-Disposition'] = "attachment; filename=export.bib"
    req.sendFile(path, getMimeType(path))
    if os.sep == '/':  # Unix?
        os.unlink(path)  # unlinking files while still reading them only works on Unix/Linux
예제 #34
0
def getContent(req, ids):

    user = users.getUserFromRequest(req)
    language = lang(req)

    def get_ids_from_query():
        ids = get_ids_from_req(req)
        return ",".join(ids)

    if "action" in req.params:
        state = 'ok'

        if req.params.get('action') == "removefiles":
            basenode = q(Node).get(req.params.get('id'))
            for f in basenode.files:
                try:
                    os.remove(f.abspath)
                    pass
                except:
                    state = "error"
            basenode.files = []
            db.session.commit()
            req.write(json.dumps({'state': state}, ensure_ascii=False))
            return None

        if req.params.get('action') == "buildnode":  # create nodes
            basenode = q(Node).get(req.params.get('id'))
            newnodes = []
            errornodes = []
            basenodefiles_processed = []
            if req.params.get('uploader', '') == 'plupload':
                filename2scheme = {}
                for k in req.params:
                    if k.startswith("scheme_"):
                        filename2scheme[k.replace('scheme_', '',
                                                  1)] = req.params.get(k)

                for f in basenode.files:
                    filename = f.name
                    if filename in filename2scheme:
                        mimetype = getMimeType(filename)

                        if mimetype[1] == "bibtex":  # bibtex import handler
                            try:
                                new_node = importBibTeX(f.abspath, basenode)
                                newnodes.append(new_node.id)
                                basenodefiles_processed.append(f)
                            except ValueError, e:
                                errornodes.append((filename, unicode(e)))

                        logg.debug("filename: %s, mimetype: %s", filename,
                                   mimetype)
                        logg.debug("__name__=%s, func=%s; _m=%s, _m[1]=%s",
                                   __name__, funcname(), mimetype, mimetype[1])

                        content_class = Node.get_class_for_typestring(
                            mimetype[1])
                        node = content_class(name=filename,
                                             schema=filename2scheme[filename])

                        basenode.children.append(node)
                        node.set("creator", user.login_name)
                        node.set(
                            "creationtime",
                            unicode(
                                time.strftime('%Y-%m-%dT%H:%M:%S',
                                              time.localtime(time.time()))))
                        # set filetype for uploaded file as requested by the content class
                        f.filetype = content_class.get_upload_filetype()
                        node.files.append(f)
                        node.event_files_changed()
                        newnodes.append(node.id)
                        basenodefiles_processed.append(f)
                        basenode.files.remove(f)
                        db.session.commit()
                        logg.info(
                            "%s created new node id=%s (name=%s, type=%s) by uploading file %s, "
                            "node is child of base node id=%s (name=%s, type=%s)",
                            user.login_name, node.id, node.name, node.type,
                            filename, basenode.id, basenode.name,
                            basenode.type)

            else:
                for filename in req.params.get('files').split('|'):
                    mimetype = getMimeType(filename)
                    logg.debug("... in %s.%s: getMimeType(filename=%s)=%s",
                               __name__, funcname(), filename, mimetype)
                    if mimetype[1] == req.params.get('type') or req.params.get(
                            'type') == 'file':
                        for f in basenode.files:
                            # ambiguity here ?
                            if f.abspath.endswith(filename):
                                # bibtex import handler
                                if mimetype[
                                        1] == "bibtex" and not req.params.get(
                                            'type') == 'file':
                                    try:
                                        new_node = importBibTeX(
                                            f.abspath, basenode)
                                        newnodes.append(new_node.id)
                                        basenodefiles_processed.append(f)
                                    except ValueError, e:
                                        errornodes.append(
                                            (filename, unicode(e)))
                                    db.session.commit()
                                else:

                                    logg.debug(
                                        "creating new node: filename: %s",
                                        filename)
                                    logg.debug("files at basenode: %s",
                                               [(x.getName(), x.abspath)
                                                for x in basenode.files])

                                    content_class = Node.get_class_for_typestring(
                                        req.params.get('type'))
                                    node = content_class(
                                        name=filename,
                                        schema=req.params.get('value'))

                                    basenode.children.append(node)
                                    node.set("creator", user.login_name)
                                    node.set(
                                        "creationtime",
                                        unicode(
                                            time.strftime(
                                                '%Y-%m-%dT%H:%M:%S',
                                                time.localtime(time.time()))))

                                    # clones to a file with random name
                                    cloned_file = importFileRandom(f.abspath)
                                    # set filetype for uploaded file as requested by the content class
                                    cloned_file.filetype = content_class.get_upload_filetype(
                                    )
                                    node.files.append(cloned_file)
                                    node.event_files_changed()
                                    newnodes.append(node.id)
                                    basenodefiles_processed.append(f)

                                    logg.info(
                                        "%s created new node id=%s (name=%s, type=%s) by uploading file %s, "
                                        "node is child of base node id=%s (name=%s, type=%s)",
                                        user.login_name, node.id, node.name,
                                        node.type, filename, basenode.id,
                                        basenode.name, basenode.type)

                                    break  # filename may not be unique

            new_tree_labels = [{
                'id': basenode.id,
                'label': getTreeLabel(basenode, lang=language)
            }]
            for f in basenodefiles_processed:
                basenode.files.remove(f)
                f_path = f.abspath
                if os.path.exists(f_path):
                    logg.debug("%s going to remove file %s from disk",
                               user.login_name, f_path)
                    os.remove(f_path)

            mime = getMimeType(filename)
            scheme_type = {mime[1]: []}
            for scheme in get_permitted_schemas():
                if mime[1] in scheme.getDatatypes():
                    scheme_type[mime[1]].append(scheme)
                    # break

            db.session.commit()
            # standard file
            content = req.getTAL('web/edit/modules/upload.html', {
                'files': [filename],
                'schemes': scheme_type
            },
                                 macro="uploadfileok")

            res = {
                'state': state,
                'newnodes': newnodes,
                'errornodes': errornodes,
                'new_tree_labels': new_tree_labels,
                'ret': content
            }
            res = json.dumps(res, ensure_ascii=False)
            req.write(res)
            return None
예제 #35
0
def filebrowser(node, req):
    filesize = 0
    ret = list()
    paths = []
    for f in node.getFiles():
        if f.getType() == "attachment":
            paths.append(f._path)
            # break

    if len(paths) == 1 and os.path.isdir(
            config.get("paths.datadir") + paths[0]):
        # single file with no path
        path = paths[0]
    elif len(paths) > 0:
        # some single files
        files = []
        for path in paths:
            file = {}
            if not os.path.isdir(config.get("paths.datadir") + path):  # file
                file["mimetype"], file["type"] = getMimeType(
                    config.get("paths.datadir") + path)
                file["icon"] = fileicons[file["mimetype"]]
                file["path"] = path
                file["name"] = f.getName()
                if os.path.exists(config.get("paths.datadir") + path):
                    size = os.path.getsize(config.get("paths.datadir") + path)
                else:
                    size = 0
                file["size"] = format_filesize(size)
                filesize += int(size)
                files.append(file)

        return files, filesize
    else:
        path = ""

    if path == "":
        # no attachment directory -> test for single file
        file = {}
        for f in node.getFiles():
            if f.getType() not in node.getSysFiles():
                file["mimetype"], file["type"] = getMimeType(f.getName())
                file["icon"] = fileicons[file["mimetype"]]
                file["path"] = f._path
                file["name"] = f.getName()
                size = f.getSize() or 0
                file["size"] = format_filesize(size)
                filesize += f.getSize()
                ret.append(file)
        return ret, filesize

    if not path.endswith("/") and not req.params.get("path",
                                                     "").startswith("/"):
        path += "/"
    path += req.params.get("path", "")

    if req.params.get("path", "") != "":
        file = {}
        file["type"] = "back"
        file["mimetype"] = "back"
        file["icon"] = fileicons[file["mimetype"]]
        file["name"] = ".."
        file["path"] = req.params.get("path", "")
        file["req_path"] = req.params.get(
            "path", "")[:req.params.get("path", "").rfind("/")]
        ret.append(file)

    for name in os.listdir(config.settings["paths.datadir"] + path + "/"):

        if name.endswith(".thumb") or name.endswith(".thumb2"):
            continue
        file = {}

        file_path = os.path.join(config.settings["paths.datadir"] + path, name)
        if os.path.isdir(file_path):
            # directory
            file["type"] = "dir"
            file["mimetype"] = "directory"
        else:
            # file
            file["mimetype"], file["type"] = getMimeType(name)
            file["size"] = format_filesize(os.path.getsize(file_path))
            filesize += os.path.getsize(file_path)

        file["icon"] = fileicons[file["mimetype"]]
        file["path"] = os.path.join(path, name)
        file["name"] = name
        file["req_path"] = req.params.get("path", "") + "/" + file["name"]
        ret.append(file)

    return ret, format_filesize(filesize)
예제 #36
0
def getContent(req, ids):
    user = current_user
    node = q(Node).get(ids[0])

    if "logo" in current_user.hidden_edit_functions or not node.has_write_access(
    ):
        req.setStatus(httpstatus.HTTP_FORBIDDEN)
        return req.getTAL("web/edit/edit.html", {}, macro="access_error")

    # delete logo file
    if "action" in req.params and req.params.get('action') == "delete":
        file = req.params.get('file').split("/")[-1]
        for f in node.files:
            if f.abspath.endswith(file):
                node.files.remove(f)
                db.session.commit()
                req.write('ok')
                return None
        req.write('not found')
        return None

    # add logo file
    if "addfile" in req.params.keys():
        file = req.params.get("updatefile")
        if file:
            mimetype = "application/x-download"
            type = "file"
            mimetype, type = getMimeType(file.filename.lower())

            if mimetype not in ("image/jpeg", "image/gif", "image/png"):
                # wrong file type (jpeg, jpg, gif, png)
                req.setStatus(httpstatus.HTTP_INTERNAL_SERVER_ERROR)
                return req.getTAL("web/edit/modules/logo.html", {},
                                  macro="filetype_error")
            else:
                file = importFile(file.filename, file.tempname)
                node.files.append(file)
                db.session.commit()

    # save logo
    if "logo_save" in req.params.keys():
        # save url
        if req.params.get("logo_link", "") == "":
            if 'url' in node.attrs:
                del node.attrs['url']
        else:
            node.set('url', req.params.get("logo_link"))

        # save filename
        if req.params.get('logo') == "/img/empty.gif":
            # remove logo from current node
            node.set("system.logo", "")
            logg.info("%s cleared logo for node %s (%s, %s)", user.login_name,
                      node.id, node.name, node.type)
        else:
            node.set("system.logo", req.params.get("logo").split("/")[-1])
            logg.info("%s set logo for node %s (%s, %s) to %s",
                      user.login_name, node.id, node.name, node.type,
                      node.get("system.logo"))

        db.session.commit()

    logofiles = []
    for f in node.files:
        if f.filetype == "image":
            logofiles.append(splitpath(f.abspath))

    v = {
        "id": req.params.get("id", "0"),
        "tab": req.params.get("tab", ""),
        "node": node,
        "logofiles": logofiles,
        "logo": node.getLogoPath(),
        "language": lang(req),
        "t": translation_t
    }

    return req.getTAL("web/edit/modules/logo.html", v, macro="edit_logo")
예제 #37
0
def send_file(req):
    parts = splitpath(req.path)
    if len(parts) != 2:
        return 400

    nidstr, filename = parts
    if nidstr.endswith("_transfer.zip"):
        nidstr = nidstr[:-13]

    nid = userinput.string_to_int(nidstr)
    if nid is None:
        return 400

    version_id = version_id_from_req(req)

    node = get_node_or_version(nid, version_id)

    if (node is None
            or isinstance(node, Container) and not node.has_read_access()
            or isinstance(node, Content) and not node.has_data_access()):
        return 404

    def _send_attachment(filepath, mimetype):
        file_ext = os.path.splitext(filepath)[1]
        if existMetaField(node.schema, u'nodename'):
            display_file_name = u'{}{}'.format(os.path.splitext(os.path.basename(node.name))[0], file_ext)
        else:
            display_file_name = filename
        try:
            display_file_name.encode('ascii')
        except UnicodeEncodeError:
            req.reply_headers["Content-Disposition"] = u'attachment; filename="{0}"; filename*=UTF-8\'\'{0}'.\
                format(quote(display_file_name.encode('utf8')))
        else:
            req.reply_headers["Content-Disposition"] = u'attachment; filename="{}"'.format(display_file_name)
        return req.sendFile(filepath, mimetype)

    if filename is None:
        # build zip-file and return it
        with tempfile.NamedTemporaryFile() as tmpfile:
            files_written = build_transferzip(tmpfile, node)
            if files_written == 0:
                return 404
            # don't enable nginx x_accel_redirect for temporary files
            return req.sendFile(tmpfile.name, "application/zip", nginx_x_accel_redirect_enabled=False)

    # try full filename
    for f in node.files:
        if f.base_name == filename:
            return _send_attachment(f.abspath, f.mimetype)

    archive = get_archive_for_node(node)
    if archive:
        filepath = archive.get_local_filepath(node)
        mimetype, _ = getMimeType(filepath)
        return _send_attachment(filepath, mimetype)

    else:
        # try only extension
        file_ext = os.path.splitext(filename)[1]
        for f in node.files:
            if os.path.splitext(f.base_name)[1] == file_ext and f.filetype in [u'document', u'original', u'mp3']:
                logg.warn("serving file %s for node %s only by matched extension", f.path, node.id)
                return _send_attachment(f.abspath, f.mimetype)

    return 404