Exemple #1
0
def upload_file(payload, ufile):
    if ufile and allowed_file(ufile.filename):
        filename = secure_filename(ufile.filename)
        path = os.path.join(".", app.config['UPLOAD_FOLDER'], filename)
        if not os.path.isfile(path):
            ufile.save(path)
            try:
                # Create thumbnail
                from PIL import Image
                size = 128, 128
                im = Image.open(path)
                width, height = im.size
                im.thumbnail(size, Image.ANTIALIAS)
                file, ext = os.path.splitext(path)
                ext = ext.lower()
                etype = ext.replace(".", "")
                if etype == 'jpg':
                    etype = 'jpeg'
                im.save(file + "_thumbnail" + ext, etype)
            except (ImportError, IOError):
                width = 0
                height = 0

            afile = File(name=filename, user_id=payload.get('user_id'), path=path, size=os.path.getsize(path), width=width, height=height, mimetype=ufile.mimetype)
            afile.insert()
            safe_commit()
            return afile.id
    return None
Exemple #2
0
def upload_file(payload, ufile):
    upload_folder = os.path.abspath(os.path.join(APP_ROOT, "../", '../', g.upload_directory))
    if ufile and allowed_file(ufile.filename):
        filename = secure_filename(ufile.filename)
        thumbnail_name = ''
        thumbnail_path = ''
        path = os.path.join(upload_folder, filename)
        print(path)
        if not os.path.isfile(path):
            ufile.save(path)
            try:
                # Create thumbnail
                from PIL import Image
                size = 128, 128
                im = Image.open(path)
                width, height = im.size
                im.thumbnail(size, Image.ANTIALIAS)
                file, ext = os.path.splitext(path)
                fname, fext = os.path.splitext(filename)
                ext = ext.lower()
                etype = ext.replace(".", "")
                if etype == 'jpg':
                    etype = 'jpeg'
                thumbnail_path = file + "_thumbnail" + ext
                thumbnail_name = fname + "_thumbnail" + ext
                im.save(thumbnail_path, etype)
            except (ImportError, IOError):
                width = 0
                height = 0

            afile = File(name=filename, path=path, thumbnail_name=thumbnail_name,
                         thumbnail_path=thumbnail_path, user_id=payload.get('user_id'),
                         size=os.path.getsize(path), width=width, height=height,
                         mimetype=ufile.mimetype)
            afile.insert()
            safe_commit()
            return afile.id
    return None
Exemple #3
0
    def test_upload(self):
        filename = 'test.txt'
        post_data = {
            'file': (StringIO("This is a test file."), filename),
            'name': 'Test File',
            'user_id': self.user.id
        }
        rv = self.app.post('/upload_ajax', data=post_data, follow_redirects=True)
        self.assertEquals(rv.status_code, 200)
        data = json.loads(rv.data)
        self.assertEquals(data['messages'][0], 'The file was uploaded.')
        afile = File.get(data['id'])
        self.assertEquals(data['id'], afile.id)
        the_file = os.path.join(impression.app.config['UPLOAD_FOLDER'], filename)

        self.assertTrue(os.path.isfile(the_file))

        # Delete the file we uploaded
        os.unlink(the_file)
Exemple #4
0
def admin_files_delete():
    return_value = success('The file has been deleted.')
    payload = get_payload(request)
    afile = File.get(payload.get('id'))
    if afile:
        try:
            os.unlink(afile.path)
        except OSError:
            pass
        try:
            os.unlink(afile.thumbnail_path)
        except OSError:
            pass
        afile.delete()
        safe_commit()
    else:
        return_value = failure('File not found.')

    return jsonify(return_value)
Exemple #5
0
    def test_upload(self):
        filename = 'test.txt'
        the_file = os.path.join(app.config['UPLOAD_FOLDER'], filename)
        if os.path.isfile(the_file):
            os.unlink(the_file)

        post_data = {
            'file': (StringIO("This is a test file."), filename),
            'name': 'Test File',
            'user_id': self.user.id
        }
        rv = self.app.post('/upload_ajax', data=post_data, follow_redirects=True)
        self.assertEquals(rv.status_code, 200)
        data = json.loads(rv.data)
        self.assertEquals(data['messages'][0], 'The file was uploaded.')
        afile = File.get(data['id'])
        self.assertEquals(data['id'], afile.id)
        self.assertTrue(os.path.isfile(the_file))

        # Delete the file we uploaded
        os.unlink(the_file)
Exemple #6
0
def admin_files_list():
    files = File.all()
    return render('admin_files_list.html', files=files)
Exemple #7
0
def admin_files_list():
    files = File.all()
    return render_admin('files_list.html', files=files)