def upload(self, dir, datafile, **kwargs): error = '' fn = '' if datafile: path = Path(op.join(datapath, dir)) if not path: path.make() fn = path + datafile.filename if not fn.is_legal: error = "'%s' is not legal" if fn and 'overwrite' not in kwargs: error = "'%s' exists already, if you want to overwrite the old version, check allow overwrite" % fn.name # Buffer file for first check encoding and secondly upload file with BytesIO(datafile.file.read()) as filebuffer: # determine file encodings result = chardet.detect(filebuffer.read()) # Reset file buffer filebuffer.seek(0) # if chardet can determine file encoding, check it and warn respectively # otherwise state not detecting # TODO: chardet cannot determine sufficent amount of encodings, such as utf-16-le if result['encoding']: file_encoding = result['encoding'].lower() # TODO: outsource valid encodings if not (file_encoding in ['utf-8', 'ascii'] or 'utf-8' in file_encoding): log.error("WARNING: encoding of file {} is {}".format( datafile.filename, file_encoding)) else: msg = "WARNING: encoding of file {} is not detectable".format( datafile.filename) log.error(msg) try: write_to_file(fn.absolute, filebuffer) fn.setownergroup() except: error += '\n' + traceback() print(error) if "uploadimport" in kwargs and not error: url = '/download/to_db?filename=' + escape(fn.href) else: url = '/download?dir=' + escape(dir) if error: url += '&error=' + escape(error) raise web.HTTPRedirect(url)
def newfolder(self, dir, newfolder): error = '' if newfolder: if ' ' in newfolder: error = "The folder name may not include a space!" else: try: path = Path(op.join(datapath, dir, newfolder)) if not path: path.make() path.setownergroup() else: error = "Folder %s exists already!" % newfolder except: error = traceback() else: error = 'Forgotten to give your new folder a name?' url = '/download?dir=' + escape(dir) if error: url += '&error=' + escape(error) return self.index(dir=dir, error=error)