def upload(self, chapter=''): print_enter('worknoteBookServer.upload') print cherrypy.request.headers from tempfile import gettempdir from shutil import copyfileobj, rmtree from os.path import join, split, exists from zipfile import ZipFile from worknoteBookHelpers import unzip_worknote print 'Receiving file...' dst_file = join(gettempdir(), 'worknoteBook_upload.zip') with open(dst_file, 'wb') as outfile: copyfileobj(cherrypy.request.body, outfile) if 'X-Worknote-Workdir' in cherrypy.request.headers: wn_dir = cherrypy.request.headers['X-Worknote-Workdir'] print 'Header set worknote directory: "{:s}"'.format(wn_dir) else: zipfile = ZipFile(dst_file, 'r') wn_dir = split(zipfile.namelist()[0])[0] zipfile.close() print 'File set worknote directory: "{:s}"'.format(wn_dir) if chapter == '': print 'Upload to default storage dir...' storagedir = self.storagedir else: print 'Upload to chapter "{:s}"...'.format(chapter) if not chapter in self.chapters: print 'ERROR: Chapter not found' return 'Fail (Chapter {:s} not found)'.format(chapter) storagedir = self.chapters[chapter]['chapter_dir'] print '\tStorage dir: "{:s}"'.format(storagedir) if 'X-Worknote-Overwrite' in cherrypy.request.headers: overwrite = cherrypy.request.headers['X-Worknote-Overwrite'] == 'True' print 'Overwrite set in header, emptying directory "{:s}"...'.format(join(storagedir, wn_dir)) if overwrite: if exists(join(storagedir, wn_dir)): rmtree(join(storagedir, wn_dir)) try: print 'Unzipping worknote...' unzip_worknote(dst_file, join(storagedir, wn_dir)) except OSError, e: print 'ERROR: Unzip failed' return 'Fail (' + str(e) + ')'
if len(index) == 1: index = '{:d}'.format(index[0]) elif len(index) == 2: index = '{:d}:{:d}'.format(index[0], index[1]) tmpfn = join(gettempdir(), 'worknoteBook_download.zip') server_url = 'http://{server:s}/download?index={index:s}'.format(server = self.get_server(servername), index = index) try: server = urlopen(server_url) with open(tmpfn, 'wb') as tmpfile: tmpfile.write(server.read()) except URLError, e: print 'ERROR: Download failed ({:s})'.format(str(e)) return try: unzip_worknote(tmpfn, workdir) except OSError, e: print 'ERROR: Unable to download file ({:s})'.format(str(e)) except IOError: with open(tmpfn, 'r') as errfile: print 'ERROR: Not a zip file ({:s})'.format(errfile.read()) def delete(self, index, servername=None): from urllib2 import Request, urlopen, HTTPError, URLError, HTTPCookieProcessor, build_opener, install_opener from base64 import b64encode from worknoteBookHelpers import parse_index from cookielib import CookieJar jar = CookieJar() opener = build_opener(HTTPCookieProcessor(jar)) install_opener(opener) if servername is None: