Example #1
0
 def test_save_dir(self):
     fu = FileUpload(open(__file__, 'rb'), 'testfile', __file__)
     dirpath = tempfile.mkdtemp()
     filepath = os.path.join(dirpath, fu.filename)
     fu.save(dirpath)
     self.assertEqual(fu.file.read(), open(filepath, 'rb').read())
     os.unlink(filepath)
     os.rmdir(dirpath)
 def test_save_dir(self):
     fu = FileUpload(open(__file__, 'rb'), 'testfile', __file__)
     dirpath = tempfile.mkdtemp()
     filepath = os.path.join(dirpath, fu.filename)
     fu.save(dirpath)
     self.assertEqual(fu.file.read(), open(filepath, 'rb').read())
     os.unlink(filepath)
     os.rmdir(dirpath)
Example #3
0
 def _upload_to_path(body, headers, path):
     file_saver = FileUpload(body, None,
                             filename=os.path.basename(path),
                             headers=headers)
     if os.path.exists(path):
         os.unlink(path)
     if not os.path.exists(os.path.dirname(path)):
         mkdir(os.path.dirname(path))
     file_saver.save(os.path.dirname(path))
Example #4
0
 def test_fromImage(self):
     pil_img = Image.new(mode='RGB', size=(32, 32))
     with tempfile.NamedTemporaryFile('wb') as wh:
         pil_img.save(wh.name, 'PNG')
         with open(wh.name, 'rb') as rh:
             # prepare fileupload
             fupload = FileUpload(rh, 'test.png', 'test.png')
             
             game = self.db.Game.fromImage(
                 gm=self.engine.main_db.GM.select(lambda g: g.url == 'url456').first(),
                 url='bar',
                 handle=fupload
             )
             
             # expect proper scenes' order
             self.assertEqual(len(game.order), 1)
             
             # assert one scene with only one token, which the background
             self.assertEqual(len(game.scenes), 1)
             scene = list(game.scenes)[0]
             tokens = self.db.Token.select(lambda t: t.scene == scene)
             self.assertEqual(len(tokens), 1)
             self.assertEqual(tokens.first().size, -1)
             
             # assert token's image exist
             img_path = self.engine.paths.getGamePath(game.gm_url, game.url)
             img_id = tokens.first().url.split('/')[-1]
             img_fname = img_path / img_id
             self.assertTrue(os.path.exists(img_fname))
Example #5
0
    def test_getSize(self):
        # create dummy file
        with tempfile.TemporaryFile() as h:
            h.write(b'0' * 4953)

            # rewind for reading
            h.seek(0)
            fupload = FileUpload(h, 'demo.dat', 'demo.dat')
            size = self.engine.getSize(fupload)
            self.assertEqual(size, 4953)
Example #6
0
def upload():
	try:
		keys = request.query
		filename = keys.get("name")
		filename = cutter.cleanfilename(filename)
		cut_from, cut_to = keys.get("cutfrom"), keys.get("cutto")
		filetype = filename.split(".")[-1]
		if filetype not in ["mkv","mp4","avi"]: return "ERROR_FILETYPE"
		FileUpload(request.body,name=None,filename=None).save("queue/" + filename)
		return cutter.add(filename,start=cut_from,end=cut_to)
	except:
		return "ERROR_GENERIC"
Example #7
0
 def test_save_buffer(self):
     fu = FileUpload(open(__file__, 'rb'), 'testfile', __file__)
     buff = BytesIO()
     fu.save(buff)
     buff.seek(0)
     self.assertEqual(fu.file.read(), buff.read())
Example #8
0
    def test_fromZip(self):
        game = self.db.Game(url='foo', gm_url='url456')
        game.postSetup()
        
        # create an empty file (to make sure it isn't blocking removing the directory)
        img_path = self.engine.paths.getGamePath(game.gm_url, game.url)
        id1 = game.getNextId()
        p1 = img_path / '{0}.png'.format(id1)
        p1.touch()
        url = game.getImageUrl(id1)
        
        # create two demo scenes with tokens
        scene1 = self.db.Scene(game=game)
        self.db.Token(scene=scene1, url=url, posx=0, posy=0, size=-1) # background
        for i in range(7):
            self.db.Token(scene=scene1, url=url, posx=200, posy=150, size=20)
        scene2 = self.db.Scene(game=game)
        for i in range(4):
            self.db.Token(scene=scene2, url=url, posx=200, posy=150, size=20)
        self.db.commit()
        
        # create zip file
        fname, path = game.toZip()
        zip_path    = path / fname
        
        # create copy of original game by importing zip
        with open(zip_path, 'rb') as fp:
            fupload = FileUpload(fp, 'demo.zip', 'demo.zip')
            
            game2 = self.db.Game.fromZip(
                gm=self.engine.main_db.GM.select(lambda g: g.url == 'url456').first(),
                url='bar',
                handle=fupload
            )
            
            # expect proper scenes' order
            self.assertEqual(len(game2.order), 2)
            
            # assert both games having the same scenes
            self.assertEqual(len(game2.scenes), len(game.scenes))
            game2_scene1 = list(game2.scenes)[0]
            game2_scene2 = list(game2.scenes)[1]
            query1 = self.db.Token.select(lambda t: t.scene == game2_scene1)
            query2 = self.db.Token.select(lambda t: t.scene == game2_scene2)
            # order isn't important here
            self.assertEqual(set([4, 8]), set([len(query1), len(query2)]))
            
            # assert all images being there
            new_img_path = self.engine.paths.getGamePath(game2.gm_url, game2.url)
            for t in query1:
                img_id = t.url.split('/')[-1]
                img_fname = new_img_path / img_id
                self.assertTrue(os.path.exists(img_fname))
            for t in query2:
                img_id = t.url.split('/')[-1]
                img_fname = new_img_path / img_id
                self.assertTrue(os.path.exists(img_fname))
            
            # @NOTE: exact token data (position etc.) isn't tested here
        
        # create corrupt json file inside zip
        with tempfile.TemporaryDirectory() as tmp_dir:
            # manipulate json
            json_path = os.path.join(tmp_dir, 'game.json')
            with open(json_path , 'w') as h:
                h.write('{some[brokenstuff": "(}]')
            
            # pack zip (without any images)
            with zipfile.ZipFile(zip_path, "w") as h:
                h.write(json_path, 'game.json')

        # try to upload that corrupted file
        with open(zip_path, 'rb') as fp:
            fupload = FileUpload(fp, 'demo.zip', 'demo.zip')
            
            game3 = self.db.Game.fromZip(
                gm=self.engine.main_db.GM.select(lambda g: g.url == 'url456').first(),
                url='bar',
                handle=fupload
            )
            self.assertIsNone(game3) 
Example #9
0
 def test_save_file(self):
     fu = FileUpload(open(__file__, 'rb'), 'testfile', __file__)
     buff = tempfile.TemporaryFile()
     fu.save(buff)
     buff.seek(0)
     self.assertEqual(fu.file.read(), buff.read())
Example #10
0
 def test_name(self):
     self.assertEqual(FileUpload(None, 'abc', None).name, 'abc')
Example #11
0
 def test_save_overwrite_lock(self):
     fu = FileUpload(open(__file__, 'rb'), 'testfile', __file__)
     self.assertRaises(IOError, fu.save, __file__)
Example #12
0
 def test_save_buffer(self):
     fu = FileUpload(open(__file__, 'rb'), 'testfile', __file__)
     buff = BytesIO()
     fu.save(buff)
     buff.seek(0)
     self.assertEqual(fu.file.read(), buff.read())
Example #13
0
 def test_save_file(self):
     fu = FileUpload(open(__file__, 'rb'), 'testfile', __file__)
     buff = tempfile.TemporaryFile()
     fu.save(buff)
     buff.seek(0)
     self.assertEqual(fu.file.read(), buff.read())
Example #14
0
    def test_upload(self):
        game = self.db.Game(url='foo', gm_url='url456')
        game.postSetup()
        
        scene = self.db.Scene(game=game)
        
        # can upload image file
        pil_img = Image.new(mode='RGB', size=(32, 32))
        with tempfile.NamedTemporaryFile('wb') as wh:
            pil_img.save(wh.name, 'PNG')
            with open(wh.name, 'rb') as rh:
                # prepare fileupload
                fupload = FileUpload(rh, 'test.png', 'test.png')
                
                # test upload result
                old_id = game.getNextId()
                url = game.upload(fupload)
                new_id = game.getNextId()
                self.assertEqual(old_id + 1, new_id)
                self.assertEqual(url, game.getImageUrl(old_id))
                
                # test file exists   
                img_path = self.engine.paths.getGamePath(game.gm_url, game.url)
                p = img_path / '{0}.png'.format(old_id)
                self.assertTrue(os.path.exists(p))
                
                # check md5 being stored
                md5 = self.engine.getMd5(fupload.file)
                checksums = self.engine.checksums[game.getUrl()]
                self.assertIn(md5, checksums)
                
                # try to reupload file: same file used
                old_id = game.getNextId()
                new_url = game.upload(fupload)
                new_id = game.getNextId()
                self.assertEqual(old_id, new_id)
                self.assertEqual(url, new_url)
        
                # can upload another image file (different to 1st one)
                pil_img2 = Image.new(mode='RGB', size=(48, 48))
                with tempfile.NamedTemporaryFile('wb') as wh:
                    pil_img2.save(wh.name, 'PNG')
                    with open(wh.name, 'rb') as rh2:
                        # upload 2nd file
                        fupload2 = FileUpload(rh2, 'test.png', 'test.png') 
                        new_id = game.getNextId()
                        url2 = game.upload(fupload2)
                        
                        # test 2nd file exists   
                        img_path2 = self.engine.paths.getGamePath(game.gm_url, game.url)
                        p2 = img_path2 / '{0}.png'.format(new_id)
                        self.assertTrue(os.path.exists(p2))
                        
                        # check 2nd md5 being stored
                        md5_2 = self.engine.getMd5(fupload2.file)
                        checksums = self.engine.checksums[game.getUrl()]
                        self.assertIn(md5_2, checksums) 
                        
                        # cleanup to delete 1st file
                        game.cleanup(0)
                        checksums = self.engine.checksums[game.getUrl()]
                        self.assertNotIn(md5, checksums)
                        self.assertFalse(os.path.exists(p))
                        self.assertIn(md5_2, checksums)
                        self.assertTrue(os.path.exists(p2))

                        # reupload 1st file            
                        p1_new = img_path2 / '{0}.png'.format(game.getNextId())
                        url = game.upload(fupload)  
                        checksums = self.engine.checksums[game.getUrl()]
                        self.assertIn(md5, checksums)
                        self.assertTrue(os.path.exists(p1_new))
                        self.assertIn(md5_2, checksums)
                        self.assertTrue(os.path.exists(p2))

        # cannot upload broken file
        with tempfile.NamedTemporaryFile('wb') as wh:
            wh.write(b'0' * 2**20)
            with open(wh.name, 'rb') as rh:
                # prepare fileupload
                fupload = FileUpload(rh, 'test.png', 'test.png')
                
                # test upload result
                old_id = game.getNextId()
                url = game.upload(fupload)
                self.assertIsNone(url)
Example #15
0
 def assertFilename(self, bad, good):
     fu = FileUpload(None, None, bad)
     self.assertEqual(fu.filename, good)
Example #16
0
 def test_content_type(self):
     fu = FileUpload(None, None, None, {"Content-type": "text/plain"})
     self.assertEqual(fu.content_type, 'text/plain')
Example #17
0
 def test_raw_filename(self):
     self.assertEqual(FileUpload(None, None, 'x/x').raw_filename, 'x/x')