Ejemplo n.º 1
0
def showEditor(path, userLogger):

    # make sure file exists
    if not os.path.isfile(path):
        templates.error(path)

    # split file from path
    filepath = os.path.dirname(path)
    filename = os.path.basename(path)

    # mime type
    mime = mimetypes.guess_type(path)

    # verify content
    if 'image/' in str(mime):
        listImages = [
            f for f in os.listdir(filepath)
            if os.path.isfile(os.path.join(filepath, f))
            and 'image/' in str(mimetypes.guess_type(f))
        ]
        listImages.sort(key=str.lower)
        currentIndex = listImages.index(filename)
        previousImage = None if currentIndex <= 0 else listImages[currentIndex
                                                                  - 1]
        nextImage = None if currentIndex + 1 >= len(
            listImages) else listImages[currentIndex + 1]
        templates.imageViewer(filepath, filename, previousImage, nextImage)
    elif 'application/zip' in str(
            mime) or 'application/x-zip-compressed' in str(mime):
        templates.unzipper(filepath, filename)
    elif 'application/pdf' in str(mime):
        templates.pdfViewer(filepath, filename)

    # open content as text
    try:
        try:
            # default encoding, utf-8
            with open(os.path.join(filepath, filename), 'r',
                      encoding='utf-8') as fhand:
                textcontent = fhand.read()
        except UnicodeDecodeError:
            # others such as windows,..
            with open(os.path.join(filepath, filename),
                      'r',
                      encoding='iso-8859-1') as fhand:
                textcontent = fhand.read()
    except Exception as e:
        templates.error(type(e).__name__ + ': ' + str(e))

    # replace special characters, i.e.: &times; => &amp;times;
    #textcontent = textcontent.replace('&times;', '&amp;times;')
    specialCharacters = re.findall('&+[a-z]+;', textcontent)
    textcontent = textcontent.replace('&amp;', '&amp;amp;')
    for character in specialCharacters:
        if character != '&amp;':
            textcontent = textcontent.replace(character,
                                              '&amp;' + character[1:])
    templates.editor(filepath, filename, textcontent)
Ejemplo n.º 2
0
	def onError(self, err):
		"""
		"""
		code, msg = err
		# log received event
		tpl = self.encapsule(db_event=templates.db(more=templates.error(code=code, msg=msg) ))
		tpl.addRaw( msg )
		self.logRecvEvent( shortEvt = "error", tplEvt = tpl )
Ejemplo n.º 3
0
        try:
            contentRaw = form.getvalue("textcontent")
            fhand = open(filepath, 'wb')
            contentRaw = contentRaw.encode('utf-8')
            # in case of DOS/macOS-formatting, change to unix
            #contentUnix = contentRaw.replace('\r\n', '\n') # DOS
            #contentUnix = contentUnix.replace('\r', '\n') # MAC os
            contentUnix = contentRaw.replace(b'\r\n', b'\n')  # DOS
            contentUnix = contentUnix.replace(b'\r', b'\n')  # MAC os
            fhand.write(contentUnix)
            fhand.close()
            if ".py" in filepath:
                mode = os.stat(filepath).st_mode
                os.chmod(filepath, mode | S_IEXEC)
        except Exception as e:
            templates.error(str(e))

# remove folder/file
elif cmd == "remove":
    recbin = getRecbin()
    userRecbin = os.path.join(recbin, userLogger.isLoggedIn())
    if not os.path.isdir(userRecbin):
        os.mkdir(userRecbin)
    if os.path.isdir(filepath) or os.path.isfile(filepath):
        try:
            destination = getUnusedName(
                os.path.join(userRecbin, os.path.basename(filepath)))
            os.rename(filepath, destination)
        except:
            pass
Ejemplo n.º 4
0
##################################################
# check commands (all write permission)

# add user
if cmd == "useradd":
    
    # receive command
    try:
        userLogger.userAdd(
            form.getvalue("name"),
            form.getvalue("password"),
            form.getvalue("pathPermission"),
            form.getvalue("accessPermission")
        )
    except:
        templates.error('Invalid arguments to add user')
    templates.usermanager(userLogger.getUserList(),
                          userLogger.getTargetUrl(),
                          msg=cmd + ' successful')
    
# delete user
elif cmd == "userdel":
    
    # receive command
    try:
        userLogger.userDelete(
            form.getvalue("name")
        )
    except:
        templates.error('Invalid argument to delete user')
    templates.usermanager(userLogger.getUserList(),
Ejemplo n.º 5
0
##################################################
# check commands (all write permission)

# save
if cmd == "save":

    # save file (from editor)
    if os.path.isfile(path):
        try:
            contentRaw = form.getvalue("textcontent")
            fhand = open(path, 'wb')
            #contentRaw = contentRaw.decode("iso-8859-1")
            if contentRaw == None:
                contentRaw = ""
            contentRaw = contentRaw.encode('utf-8')
            # in case of DOS/macOS-formatting, change to unix
            contentUnix = contentRaw.replace(b'\r\n', b'\n')  # DOS
            contentUnix = contentUnix.replace(b'\r', b'\n')  # MAC os
            fhand.write(contentUnix)
            fhand.close()
            #if ".py" in path:
            #        mode = os.stat(path).st_mode
            #        os.chmod(path, mode|S_IEXEC )
        except Exception as e:
            pass
            templates.error(str(e))

# show editor
showEditor(path, userLogger)
Ejemplo n.º 6
0
    print("")
    sys.stdout.flush()
    sys.stdout.buffer.write(open(fullname, "rb").read())


if os.path.isfile(file):
    createDownload(file, inline=(isInline))
elif os.path.isdir(file):

    def zipdir(path, ziph, ignore):
        for root, dirs, files in os.walk(path):
            if ignore in root:
                continue
            for file in files:
                try:
                    absPath = os.path.join(root, file)
                    ziph.write(absPath,
                               os.path.relpath(absPath, os.path.dirname(path)))
                except PermissionError as e:
                    pass

    recbin = getRecbin()
    tmpZipName = getUnusedName(os.path.join(recbin, 'tmpZip.zip'))
    zipf = zipfile.ZipFile(tmpZipName, 'w', zipfile.ZIP_DEFLATED)
    zipdir(file, zipf, recbin)
    zipf.close()
    createDownload(tmpZipName, name=(os.path.basename(file) + '.zip'))
    os.remove(tmpZipName)
else:
    templates.error(file)