示例#1
0
 def test_save_folders(self):
     uset = UploadSet('files')
     uset._config = Config('/uploads')
     tfs = TestingFileStorage(filename='foo.txt')
     res = uset.save(tfs, folder='someguy')
     assert res == 'someguy/foo.txt'
     assert tfs.saved == '/uploads/someguy/foo.txt'
示例#2
0
 def test_implicit_folder(self):
     uset = UploadSet('files')
     uset._config = Config('/uploads')
     tfs = TestingFileStorage(filename='boat.jpg')
     res = uset.save(tfs, name='someguy/photo_123.')
     assert res == 'someguy/photo_123.jpg'
     assert tfs.saved == '/uploads/someguy/photo_123.jpg'
示例#3
0
 def test_save_namedext(self):
     uset = UploadSet('files')
     uset._config = Config('/uploads')
     tfs = TestingFileStorage(filename='boat.jpg')
     res = uset.save(tfs, name='photo_123.')
     assert res == 'photo_123.jpg'
     assert tfs.saved == '/uploads/photo_123.jpg'
 def test_conflict_without_extension(self) -> None:
     uset = UploadSet('files', extensions=(''))
     uset._config = Config('/uploads')
     tfs = TestingFileStorage(filename='foo')
     self.extant('/uploads/foo')
     res = uset.save(tfs)
     assert res == 'foo_1'
示例#5
0
 def test_conflict(self):
     uset = UploadSet('files')
     uset._config = Config('/uploads')
     tfs = TestingFileStorage(filename='foo.txt')
     self.extant('/uploads/foo.txt')
     res = uset.save(tfs)
     assert res == 'foo_1.txt'
 def test_file_not_allowed(self) -> None:
     """Raise UploadNotAllowed for not allowed file extensions."""
     uset = UploadSet('files', ('png'))
     uset._config = Config('/uploads')
     testing_filestorage = TestingFileStorage(filename='picture.gif')
     with pytest.raises(UploadNotAllowed):
         uset.save(testing_filestorage)
 def test_folder_namedext(self) -> None:
     uset = UploadSet('files')
     uset._config = Config('/uploads')
     tfs = TestingFileStorage(filename='boat.jpg')
     res = uset.save(tfs, folder='someguy', name='photo_123.')
     assert res == 'someguy/photo_123.jpg'
     assert tfs.saved == '/uploads/someguy/photo_123.jpg'
示例#8
0
 def test_save_named(self):
     uset = UploadSet('files')
     uset._config = Config('/uploads')
     tfs = TestingFileStorage(filename='foo.txt')
     res = uset.save(tfs, name='file_123.txt')
     assert res == 'file_123.txt'
     assert tfs.saved == '/uploads/file_123.txt'
示例#9
0
 def test_filenames(self):
     uset = UploadSet('files')
     uset._config = Config('/uploads')
     namepairs = (('foo.txt', True), ('boat.jpg', True), ('warez.exe',
                                                          False))
     for name, result in namepairs:
         tfs = TestingFileStorage(filename=name)
         assert uset.file_allowed(tfs, name) is result
示例#10
0
 def test_multi_conflict(self):
     uset = UploadSet('files')
     uset._config = Config('/uploads')
     tfs = TestingFileStorage(filename='foo.txt')
     self.extant('/uploads/foo.txt',
                 *('/uploads/foo_%d.txt' % n for n in range(1, 6)))
     res = uset.save(tfs)
     assert res == 'foo_6.txt'
示例#11
0
 def test_conflict_without_extension(self):
     # Test case for issue #7.
     uset = UploadSet('files', extensions=(''))
     uset._config = Config('/uploads')
     tfs = TestingFileStorage(filename='foo')
     self.extant('/uploads/foo')
     res = uset.save(tfs)
     assert res == 'foo_1'
示例#12
0
 def test_non_ascii_filename(self):
     uset = UploadSet('files')
     uset._config = Config('/uploads')
     tfs = TestingFileStorage(filename=u'天安门.jpg')
     res = uset.save(tfs)
     assert res == 'jpg'
     res = uset.save(tfs, name='secret.')
     assert res == 'secret.jpg'
示例#13
0
 def test_path(self):
     uset = UploadSet('files')
     uset._config = Config('/uploads')
     assert uset.path('foo.txt') == '/uploads/foo.txt'
     assert uset.path('someguy/foo.txt') == '/uploads/someguy/foo.txt'
     assert (uset.path('foo.txt', folder='someguy') ==
             '/uploads/someguy/foo.txt')
     assert (uset.path('foo/bar.txt', folder='someguy') ==
             '/uploads/someguy/foo/bar.txt')
    def test_storage_is_not_a_werkzeug_datastructure(self) -> None:
        """UploadSet.save needs a valid FileStorage object.

        When something different is passed in, a TypeError gets raised.
        """
        uset = UploadSet('files', ALL)
        uset._config = Config('/uploads')
        non_storage = 'this is no werkzeug.datastructure.FileStorage'

        with pytest.raises(TypeError):
            uset.save(non_storage)  # type: ignore
示例#15
0
 def test_secured_filename(self):
     uset = UploadSet('files', ALL)
     uset._config = Config('/uploads')
     tfs1 = TestingFileStorage(filename='/etc/passwd')
     tfs2 = TestingFileStorage(filename='../../myapp.wsgi')
     res1 = uset.save(tfs1)
     assert res1 == 'etc_passwd'
     assert tfs1.saved == '/uploads/etc_passwd'
     res2 = uset.save(tfs2)
     assert res2 == 'myapp.wsgi'
     assert tfs2.saved == '/uploads/myapp.wsgi'
示例#16
0
 def test_filenames(self):
     uset = UploadSet('files')
     uset._config = Config('/uploads')
     namepairs = (
         ('foo.txt', True),
         ('boat.jpg', True),
         ('warez.exe', False)
     )
     for name, result in namepairs:
         tfs = TestingFileStorage(filename=name)
         assert uset.file_allowed(tfs, name) is result
示例#17
0
 def test_default_extensions(self):
     uset = UploadSet('files')
     uset._config = Config('/uploads')
     extpairs = (('txt', True), ('jpg', True), ('exe', False))
     for ext, result in extpairs:
         assert uset.extension_allowed(ext) is result
示例#18
0
文件: server.py 项目: janastu/mouchak
                   render_template, redirect, url_for, send_from_directory,
                   flash, abort)
from flask.ext.pymongo import PyMongo
from flask_uploads import (UploadSet, configure_uploads, IMAGES,
                              DATA, DOCUMENTS, UploadConfiguration)
from logging import FileHandler
from werkzeug import secure_filename
from utilities import ObjectIdCleaner


app = Flask(__name__)
app.config.from_pyfile('conf.py')
mongo = PyMongo(app)
plugin_upload = UploadSet('plugins', ('js', 'css'),
                          default_dest=lambda app: app.instance_path)
plugin_upload._config = UploadConfiguration(app.config.get(
    'PLUGIN_UPLOAD_FOLDER'))


files_upload = UploadSet('files', IMAGES + DOCUMENTS + DATA + ('pdf',),
                         default_dest=lambda app: app.instance_path)
files_upload._config = UploadConfiguration(app.config.get(
    'FILE_UPLOAD_FOLDER'))

configure_uploads(app, plugin_upload)
configure_uploads(app, files_upload)
app.register_blueprint(cache.cache, url_prefix='/cache')


bson.ObjId = bson.objectid.ObjectId  # handy reference to otherwise long name

示例#19
0
from flask import (Flask, make_response, request, jsonify, session,
                   render_template, redirect, url_for, send_from_directory,
                   flash, abort)
from flask.ext.pymongo import PyMongo
from flask_uploads import (UploadSet, configure_uploads, IMAGES, DATA,
                           DOCUMENTS, UploadConfiguration)
from logging import FileHandler
from werkzeug import secure_filename
from utilities import ObjectIdCleaner

app = Flask(__name__)
app.config.from_pyfile('conf.py')
mongo = PyMongo(app)
plugin_upload = UploadSet('plugins', ('js', 'css'),
                          default_dest=lambda app: app.instance_path)
plugin_upload._config = UploadConfiguration(
    app.config.get('PLUGIN_UPLOAD_FOLDER'))

files_upload = UploadSet('files',
                         IMAGES + DOCUMENTS + DATA + ('pdf', ),
                         default_dest=lambda app: app.instance_path)
files_upload._config = UploadConfiguration(
    app.config.get('FILE_UPLOAD_FOLDER'))

configure_uploads(app, plugin_upload)
configure_uploads(app, files_upload)
app.register_blueprint(cache.cache, url_prefix='/cache')

bson.ObjId = bson.objectid.ObjectId  # handy reference to otherwise long name


def getContent(key=None, drafts=False):
示例#20
0
def addfilesnalysislist():
    # dupCheckResult = db_session.query(Account).filter_by(id=id).first()
    # if dupCheckResult is not None:
    #     raise InvalidUsage('중복된 아이디가 있습니다.', status_code=500)
    try:
        if request.files['files'] is not None:
            Config = UploadConfiguration
            uset = UploadSet('files', ALL)
            #uset._config = Config('/usr/gsp/web_uploads')
            uploadpath = app.config['UPLOAD_FOLDER']
            subpath = datetime.datetime.now().strftime('%Y%m%d')
            combinepath = os.path.join(uploadpath,
                                       app.config['UPLOAD_CRAWLING_FOLDER'],
                                       subpath)
            uset._config = Config(combinepath)

            #DB 데이터 입력
            fileAnalysis = Rules_FileAnalysis()
            fileAnalysis.realfilename = uset.save(request.files['files'])
            fileAnalysis.orgfilename = request.files['files'].filename
            fileAnalysis.subpath = subpath
            fileAnalysis.description = request.form.get('desc')
            fileAnalysis.cre_id = session['id']
            db_session.add(fileAnalysis)
            db_session.commit()

            #elastic search 데이터 입력
            es = Elasticsearch([{
                'host': app.config['ELASTICSEARCH_URI'],
                'port': app.config['ELASTICSEARCH_PORT']
            }])
            doc = {
                '@timestamp':
                datetime.datetime.utcnow().isoformat(),
                'fileExt':
                os.path.splitext(fileAnalysis.orgfilename)[1],
                'nfs_path':
                ".\\{0}\\{1}\\{2}".format(app.config['UPLOAD_CRAWLING_FOLDER'],
                                          subpath, fileAnalysis.realfilename,
                                          fileAnalysis.realfilename),
                'data_type':
                "crawlings",
                'filePath':
                os.path.join(combinepath, fileAnalysis.realfilename),
                'fileName':
                fileAnalysis.realfilename,
                'origianl_fileName':
                fileAnalysis.orgfilename,
                'register_path':
                "001",
                'status':
                0
            }
            _index = 'gsp-{0}'.format(
                datetime.datetime.now().strftime("%Y.%m.%d"))
            es.index(index=_index, doc_type="url_crawleds", body=doc)

            return ""
    except Exception as e:
        db_session.rollback()
        raise InvalidUsage('DB 저장 오류', status_code=501)

    return ""