示例#1
0
文件: driver.py 项目: ChItZZZ/Project
def check_register():
    try:
        check_code = session.get('check_code')
        if check_code != request.form['check_code']:
            return '<script>alert("验证码错误!");location.href="/driver/register"</script>'
        user_id = request.form['userID']
        phone = session['register_phone']
        user_name = request.form['user_name']
        password = request.form['password']
        ID_card_image = request.files['ID_card_image']
        permit_card_image = request.files['permit_card_image']
        car_image = request.files['car_image']
        ID_filename = secure_filename(ID_card_image.filename)
        permit_filename = secure_filename(permit_card_image.filename)
        car_pic_filename = secure_filename(car_image.filename)
        filename = []
        filename.append(ID_filename)
        filename.append(permit_filename)
        filename.append(car_pic_filename)
        for f in filename:
            if '.' not in f or f.rsplit('.', 1)[1] not in app.config['ALLOW_FILE']:
                return '<script>alert("非法后缀!");location.href="/driver/register"</script>'
        ID_card_image.save(os.path.join(app.root_path, 'static/image/ID_card', ID_filename))
        permit_card_image.save(os.path.join(app.root_path, 'static/image/permit_card', permit_filename))
        car_image.save(os.path.join(app.root_path, 'static/image/car', car_pic_filename))
        u = driver_account(phone, password, user_name, user_id, ID_filename, permit_filename, car_pic_filename)
        db.session.add(u)
        db.session.commit()
        session.clear()
        return '<script>alert("注册成功!");location.href="/driver/login"</script>'
    except:
        return '<script>alert("未知错误,请重试!");location.href="/driver/register"</script>'
示例#2
0
def edit_file():
    if request.method == 'GET':
        fname = request.args.get('n')
        fsize = "%d kB" % math.trunc(os.path.getsize(os.path.join(files_dir_os, fname)) / 1024)
        events = TennisEvent.get_events_with_att(fname)
        src = EventSource.get(os.path.join(fname[:4], fname[5:]))  # Todo: od klicu sestavim z '/', ne glede na OS
        title = src['desc'] if src else ""
        view = src['view'] if src and src in ['0', '1'] else "1"
        # log_info("view=%s." % view)
        players = src['players_on_pic'] if src else ""
        return render_template("editFile.html", year=fname[:4], fname=fname[5:], fsize=fsize,
                               years=TennisEvent.Years, events=events, title=title, view=view, players=players)
    elif request.method == 'POST':
        old_year, old_fname = request.form['old_year'], request.form['old_fname']
        new_year = secure_filename(request.form.get('new_year') or old_year)
        new_fname = secure_filename(request.form['new_fname'])
        old_title, new_title = request.form['old_title'], request.form['new_title']
        old_view, new_view = request.form['old_view'], request.form['new_view']
        old_players, new_players = request.form['old_players'], request.form['new_players']
        old_att = os.path.join(files_dir_os, old_year, old_fname)
        new_att = os.path.join(files_dir_os, new_year, new_fname)

        if request.form["Status"][:5] == unicode("Popravi"[:5]):
            log_info("Audit: rename file %s/%s to %s/%s" % (old_year, old_fname, new_year, new_fname))
            os.rename(old_att, new_att)
            TennisEvent.update_all_atts(old_year, old_fname, new_fname)
            # ToDo: delete old entry, if necessary (year | file_name changes)
            s = EventSource(file_name=os.path.join(new_year, new_fname),
                            desc=new_title, view=new_view, players_on_pic=new_players)
            s.update()
        elif request.form["Status"][:5] == unicode("Kopiraj"[:5]):
            log_info("Audit: copy file %s/%s to %s/%s" % (old_year, old_fname, new_year, new_fname))
            shutil.copyfile(old_att, new_att)

        return redirect(request.args.get("next") or url_for("tennis_events_old"))
def uploadBathymetry(region):
	if not isAuth(request.headers):
		return jsonify(success=False, message="Forbidden"), 403
	
	if not ("dataFile" in request.files):
		return jsonify(success=False, message="No input file provided"), 400
	
	bathyFile = request.files["dataFile"]

	result = False
	if bathyFile and not (bathyFile.filename == ""):
		try:
			fullPath = os.path.join(app.config['UPLOAD_FOLDER'], secure_filename("{}_{}".format(int(time.time()), bathyFile.filename)))
			bathyFile.save(fullPath)
			try:
				result = importBathymetry(fullPath, os.path.join("data", secure_filename("{}.json".format(region))))
			except:
				pass
			os.remove(fullPath)
		except:
			pass

	try:
		bathyFile.close()
	except:
		pass
	
	return jsonify(success=result, message="Bathymetry added" if result else "Could not parse/add input file"), 200 if result else 500
示例#4
0
文件: upload.py 项目: bcdev/GISportal
def shapefile_upload():
    shapefiles = request.files.getlist('shapefile[]')
    if len(shapefiles) == 0:
        # todo -- not supposed to come here; if this is keeps not being printed out, remove the whole if-clause
        print('Apparently uploaded empty list of files...?')
        return '200'

    shapefile_name = secure_filename(shapefiles[0].filename)
    shapefile_name = shapefile_name[:shapefile_name.index('.')] + '.shp'
    path = str(os.path.join(get_shape_path(), shapefile_name))
    already_registered = Shapefile.query.filter(Shapefile.path == path).count() > 0
    if already_registered:
        print('Shapefile \''+  shapefile_name + '\' has already been uploaded before.')
        return '200'

    if not os.path.exists(get_shape_path()):
        os.makedirs(get_shape_path())

    for file in shapefiles:
        filename = secure_filename(file.filename)
        target = os.path.join(get_shape_path(), filename)
        file.save(target)
        print('Successfully uploaded file \'' + target + '\'')
    persist(shapefile_name, path)

    return '200'
示例#5
0
def test_secure_filename():
    assert utils.secure_filename('My cool movie.mov') == 'My_cool_movie.mov'
    assert utils.secure_filename('../../../etc/passwd') == 'etc_passwd'
    assert utils.secure_filename(u'i contain cool \xfcml\xe4uts.txt') == \
        'i_contain_cool_umlauts.txt'
    assert utils.secure_filename('__filename__') == 'filename'
    assert utils.secure_filename('foo$&^*)bar') == 'foobar'
示例#6
0
def get_ldd(campaign_model):
	env = dict(os.environ)
	if 'LD_LIBRARY_PATH' in env:
		env['LD_LIBRARY_PATH'] = ':' + env['LD_LIBRARY_PATH']
	else:
		env['LD_LIBRARY_PATH'] = ''
	env['LD_LIBRARY_PATH'] = os.path.join(current_app.config['DATA_DIRECTORY'], secure_filename(campaign_model.name), 'libraries') + env['LD_LIBRARY_PATH']
	try:
		p = subprocess.Popen(['ldd', os.path.join(current_app.config['DATA_DIRECTORY'], secure_filename(campaign_model.name), 'executable')], env=env, stdout=subprocess.PIPE)
		process_output = p.communicate()
	except FileNotFoundError:
		ldd = None
	else:
		ldd = []
		for line in process_output[0].decode('ascii').split('\n'):
			if not line or line[0] != '\t':
				continue
			parts = line.split()
			if len(parts) < 3:
				continue
			found = 'not found' not in line
			if found:
				path = parts[2]
				if path.startswith(current_app.config['DATA_DIRECTORY']):
					ldd_row = (parts[0], 'info', path.rsplit(os.path.sep, 1)[-1])
				else:
					ldd_row = (parts[0], '', path)
			else:
				ldd_row = (parts[0], 'danger', 'Not Found')
			if ldd_row not in ldd:
				ldd.append(ldd_row)
	return ldd
示例#7
0
 def test_secure_filename(self):
     self.assert_equal(utils.secure_filename('My cool movie.mov'),
                       'My_cool_movie.mov')
     self.assert_equal(utils.secure_filename('../../../etc/passwd'),
                       'etc_passwd')
     self.assert_equal(utils.secure_filename(u'i contain cool \xfcml\xe4uts.txt'),
                       'i_contain_cool_umlauts.txt')
示例#8
0
def upload(app, url):
    if 'file' not in request.files:
        raise InvalidUsage('No file attached')
    file = request.files['file']
    if secure_filename(file.filename)[-4:] !='.zip':
        raise InvalidUsage('The uploaded file is not a zipfile.')
    target_folder = join(app.projects_path(), secure_filename(file.filename)[:-4])
    if not commonprefix([target_folder, app.projects_path()]) == app.projects_path(): # out of the server folder
        raise InvalidUsage('Invalid filename')
    if not exists(target_folder):
        makedirs(target_folder)
    else:
        raise InvalidUsage('Project of the same name already exists.')
    try:
        zip_ref = zipfile.ZipFile(file, 'r')
    except:
        raise InvalidUsage('The file ' + file + ' is not recognised as a zip file.')
    try:
        zip_ref.extractall(target_folder)
    except:
        raise InvalidUsage('The extraction of the file ' + file + ' failed.')
    if not exists(join(target_folder, configure_filename)):
        shutil.rmtree(target_folder)
        raise InvalidUsage('The file does not contain a tremppi project.')
    write_projects(app.projects_path())
    return 'upload successful'
示例#9
0
def stl_upload_temp():
    f = request.files['the_file']
    f.save(os.path.join(TEMPORARY_UPLOAD_PATH, secure_filename(f.filename)))
    stl_object = stlutils.get_stl_object_by_name(os.path.join(TEMPORARY_UPLOAD_PATH, secure_filename(f.filename)))
    scale_dict = stlutils.get_object_scale(stl_object)
    os.remove(os.path.join(TEMPORARY_UPLOAD_PATH, secure_filename(f.filename)))
    return json.dumps(scale_dict)
示例#10
0
def upload_image():
    if request.method == "POST":
        f = request.files["file"]
        bf = request.files["blurred_file"]
        if f and bf:
            filename = os.path.join(app.config["UPLOAD_FOLDER"],
                    secure_filename(f.filename))
            blurred_filename = os.path.join(app.config["UPLOAD_FOLDER"],
					secure_filename(bf.filename))
            f.save(filename)
            bf.save(blurred_filename)
            if query_db("select * from images where title = ?",
                    [request.form["title"]], one=True) is None:
                db = get_db()
                # TODO: We should actually have a check box for has nudity and
                # also accept a second file upload for the blurred file
                db.execute("""insert into images (title, artist, work_type,
                    culture, has_nudity, filename, blurred_filename)
                    values (?, ?, ?, ?, ?, ?, ?)""", [request.form["title"],
                        request.form["artist"], request.form["work_type"],
                        request.form["culture"], False, filename, blurred_filename])
                db.commit()
        return redirect("/upload")

    return """
示例#11
0
def upload():
    """ File uploading handler. """
    url = request.args.get('url', None)
    delete = request.args.get('delete', None)
    if request.method == 'POST':
        file = request.files['file']
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(safe_join(app.config['UPLOAD_FOLDER'], filename))
            flash('File ' + filename + ' was uploaded!', 'info')
        else:
            flash('File was not uploaded!', 'error')
    if url is not None:
        file = file_from_url(url)
        if file and allowed_file(file['name']):
            file_name = secure_filename(file['name'])
            file_path = safe_join(app.config['UPLOAD_FOLDER'], file_name)
            with open(file_path, 'wb') as f:
                f.write(file['content'])
            flash('File ' + file_name + ' was uploaded!', 'info')
        else:
            flash('Can not download file!', 'error')
    if delete is not None:
        file = safe_join(app.config['UPLOAD_FOLDER'], delete)
        try:
            os.remove(file)
            flash('Success!', 'info')
        except FileNotFoundError:
            flash('File not found!', 'error')
        except Exception:
            abort(500)
    files = show_files()
    return render_template('upload.html', context={"files": files})
示例#12
0
文件: __init__.py 项目: ap13p/elearn
def tugas_create():
    form = TugasForm(request.form)
    if form.validate_on_submit():
        tugas = Tugas()
        form.populate_obj(tugas)
        user = g.user
        matkul = MataKuliah.get(MataKuliah.dosen == user)
        tugas.mata_kuliah = matkul
        tugas.save()

        file_list = request.files.getlist('file_pendukung')
        for f in file_list:
            if isinstance(f, FileStorage) and len(f.filename) != 0:
                import os
                path = generate_path()
                save_to = os.path.join(path, secure_filename(user.email))
                if not os.path.exists(save_to):
                    os.makedirs(save_to)
                tujuan = os.path.join(save_to, secure_filename(f.filename))
                f.save(tujuan)
                p = Phile.create(
                    filename=f.filename,
                    filepath=tujuan,
                    filetype=f.mimetype
                )
                TugasFile.create(tugas=tugas, phile=p)
        flash('Sukses membuat tugas')
        return redirect(url_for('dosen:tugas:list'))
    return render_template('dosen/tugas/create.html', form=form)
示例#13
0
def movie_add():
    form = MovieForm()
    if form.validate_on_submit():
        data = form.data
        file_url = secure_filename(form.url.data.filename)
        file_logo = secure_filename(form.logo.data.filename)
        if not os.path.exists(app.config["UP_DIR"]):
            os.makedirs(app.config["UP_DIR"])
            os.chmod(app.config["UP_DIR"], "rw")
        url = change_filename(file_url)
        logo = change_filename(file_logo)
        form.url.data.save(app.config["UP_DIR"] + url)
        form.logo.data.save(app.config["UP_DIR"] + logo)
        movie = Movie(
            title=data['title'],
            url=url,
            info=data["info"],
            logo=logo,
            star=int(data["star"]),
            playnum=0,
            commentnum=0,
            tag_id=int(data["tag_id"]),
            area=data["area"],
            release_time=data["release_time"],
            length=data["length"],
        )
        db.session.add(movie)
        db.session.commit()
        flash("添加电影成功!", "ok")
        return redirect(url_for('admin.movie_add'))
    return render_template("admin/movie_add.html", form=form)
示例#14
0
    def post():
        """
        Upload new wallet
        """
        parser = reqparse.RequestParser()
        parser.add_argument('filename', required=True, help='File name', location="form")
        parser.add_argument('file', type=FileStorage, required=True, help='File', location='files')
        args = parser.parse_args()

        file_path = os.path.join(current_app.config['UPLOAD_FOLDER'], secure_filename(args['filename']))
        args['file'].save(file_path)

        bsd_db = db.DB()
        try:
            bsd_db.open(file_path, flags=db.DB_RDONLY)
        except db.DBError:
            os.unlink(file_path)
            return {'result_status': False, 'message': 'File damaged or incorrect'}, 500

        new_wallet = Wallets(user_id=current_user.id,
                             name=secure_filename(args['filename']),
                             path=file_path)
        connection.session.add(new_wallet)
        connection.session.commit()

        return {'result_status': True, 'result': 'Uploaded'}, 201
def import_bookmarks():
    """Import bookmarks from file with json format."""
    if request.method == 'POST':
        filefile = request.files['file']
        if filefile:
            secure_filename(filefile.filename)
            data = filefile.read()
            try:
                decoded_data = data.decode('unicode_escape')
                json_data = json.loads(decoded_data)
                for category_name, value in json_data.items():
                    category = Category(name=category_name.lower())
                    db.session.add(category)
                    db.session.flush()
                    db.session.refresh(category)
                    if isinstance(value, list):
                        for link in value:
                            bookmark = Bookmark(
                                title=urlparse(link).netloc, url=link,
                                category_id=category.id, user_id=g.user.id)
                            db.session.add(bookmark)
                    elif isinstance(value, dict):
                        for title, link in value.items():
                            bookmark = Bookmark(title=title, url=link,
                                                category_id=category.id,
                                                user_id=g.user.id)
                            db.session.add(bookmark)
                db.session.commit()
            except Exception as e:
                db.session.rollback()
                with open('my_error_log.txt') as fob:
                    fob.write(str(e))
    return render_template('bookmarks/import_bookmarks.html')
示例#16
0
文件: photo.py 项目: tylertreat/Kanna
def upload(upload_file, image_data, user):
    """Upload a photo to the blobstore and create a photo entity to index it.

    Args:
        upload_file: the file the user uploaded.
        image_data: the raw file data to be stored as a blob.
        user: the user who uploaded the file.

    Returns:
        the photo entity for the uploaded file.
    """

    filename = secure_filename(upload_file.filename)
    blob_key = _write_to_blobstore(filename, image_data,
                                   upload_file.content_type)

    image = Image.open(StringIO(image_data))

    thumbnail = _create_circle_thumbnail(image)
    thumb_filename = secure_filename('%s_thumb' % upload_file.filename)
    thumb_key = _write_to_blobstore(thumb_filename, thumbnail, 'image/png')

    exif_data = exif.get_exif_data(image)
    lat, lon = exif.get_lat_lon(exif_data)
    coordinates = None if not lat or not lon else ndb.GeoPt(lat, lon)
    timestamp = exif.get_datetime(exif_data)

    photo = Photo(name=filename, owner=user.key, primary_blob_key=blob_key,
                  thumb_blob_key=thumb_key, coordinates=coordinates,
                  timestamp=timestamp)
    photo.put()

    return photo
示例#17
0
文件: app.py 项目: ChItZZZ/Project
def check_register():
    driver = driver_account.query.filter(driver_account.phone == session['register_phone'])
    if driver != []:
        return json.dumps({'status': '310'})
    try:
        user_id = request.values.get('userID')  # 身份证号
        phone = session['register_phone']
        user_name = request.values.get('user_name')  # 用户名
        password = request.values.get('password')  # 密码
        ID_card_image = request.files['ID_card_image']  # 身份证照片
        permit_card_image = request.files['permit_card_image']  # 驾驶证照片
        car_image = request.files['car_image']  # 行驶证照片
        ID_filename = secure_filename(ID_card_image.filename)
        permit_filename = secure_filename(permit_card_image.filename)
        car_pic_filename = secure_filename(car_image.filename)
        filename = []
        filename.append(ID_filename)
        filename.append(permit_filename)
        filename.append(car_pic_filename)
        for f in filename:
            if '.' not in f or f.rsplit('.', 1)[1] not in app.config['ALLOW_FILE']:
                return json.dumps({'status': '404'})
        ID_card_image.save(os.path.join(app.root_path, 'static/image/ID_card', ID_filename))
        permit_card_image.save(os.path.join(app.root_path, 'static/image/permit_card', permit_filename))
        car_image.save(os.path.join(app.root_path, 'static/image/car', car_pic_filename))
        u = driver_account(phone, password, user_name, user_id, ID_filename, permit_filename, car_pic_filename)
        db.session.add(u)
        db.session.commit()
        return json.dumps({'status': '340'})
    except:
        return json.dumps({'status': '350'})
示例#18
0
文件: views.py 项目: kuruwan87/idea
def upload_file():
    if not session.get('logged_in'):
        abort(404)
    # List images os.listdir(os.path.join(getcwd(),'static/'))
    images = os.listdir(app.config['UPLOAD_FOLDER'])
    if request.method == 'POST':
        try:
            file = request.files['file']
        except RequestEntityTooLarge:
            abort(413)
        if not file:
            error = u'Select a file please.'
            return render_template('upload.html', **locals())
        # file ok, type ok
        elif file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            info = u'The file {} has been successfully uploaded.'.format(filename)
            return render_template('upload.html', **locals())
        # file ok, type ko
        elif file and  not allowed_file(file.filename):
            filename = secure_filename(file.filename)
            error = u'File extension {} not allowed'.format(filename)
            return render_template('upload.html', **locals())
    return render_template('upload.html', images=images)
示例#19
0
文件: views.py 项目: kyspy/DailyTrak
def edit_waterproofing(id):
    report = Report.query.get(id)
    form = ReportForm(obj=report)
    if request.method == 'POST':
        if request.form['action'] == 'Upload':
            form.populate_obj(report)
            bim_file = form.bimimg.data
            if bim_file and allowed_file(bim_file.filename):
                bim_filename = secure_filename(str(uuid.uuid4()) + bim_file.filename)
                bim_file.save(os.path.join(app.config['UPLOAD_FOLDER'], bim_filename))
                report.bimimg_filename = bim_filename
            site_file = form.siteimg.data
            if site_file and allowed_file(site_file.filename):
                site_filename = secure_filename(str(uuid.uuid4()) + site_file.filename)
                site_file.save(os.path.join(app.config['UPLOAD_FOLDER'], site_filename))
                report.siteimg_filename = site_filename
            report.site_caption = form.site_caption.data
            report.date = form.date.data
            report.note = form.note.data
            report.summary = form.summary.data
            db.session.commit()
            return redirect(url_for('report_waterproofing'))
        elif request.form['action'] == 'Edit Previous':
            id_object = form.edit_date.data
            r = Report.query.get(id_object.id)
            id = r.id
            return redirect(url_for('edit_waterproofing', id=id))
    elif request.method == 'GET':
        return render_template('create_waterproofing.html', form=form, data_type=report.date, action="Edit")
示例#20
0
 def _make_zip(self, project, ty):
     name = self._project_name_latin_encoded(project)
     dataframe = self._respond_csv(ty, project.id)
     if dataframe is not None:
         info_dataframe = self._respond_csv(ty, project.id, info_only=True)
         datafile = tempfile.NamedTemporaryFile()
         info_datafile = tempfile.NamedTemporaryFile()
         try:
             dataframe.to_csv(datafile, index=False,
                              encoding='utf-8')
             info_dataframe.to_csv(
                 info_datafile, index=False, encoding='utf-8')
             datafile.flush()
             info_datafile.flush()
             zipped_datafile = tempfile.NamedTemporaryFile()
             try:
                 _zip = self._zip_factory(zipped_datafile.name)
                 _zip.write(
                     datafile.name, secure_filename('%s_%s.csv' % (name, ty)))
                 _zip.write(
                     info_datafile.name, secure_filename('%s_%s_info_only.csv' % (name, ty)))
                 _zip.close()
                 container = "user_%d" % project.owner_id
                 _file = FileStorage(
                     filename=self.download_name(project, ty), stream=zipped_datafile)
                 uploader.upload_file(_file, container=container)
             finally:
                 zipped_datafile.close()
         finally:
             datafile.close()
             info_datafile.close()
示例#21
0
文件: views.py 项目: kyspy/DailyTrak
def create_waterproofing():
    #figure out how to resize the image
    form = ReportForm()
    if request.method == 'POST':
        if request.form['action'] == 'Upload':
            bim_file = form.bimimg.data
            if bim_file and allowed_file(bim_file.filename):
                bim_filename = secure_filename(str(uuid.uuid4()) + bim_file.filename)
                bim_file.save(os.path.join(app.config['UPLOAD_FOLDER'], bim_filename))
            else:
                bim_filename = ""
            site_file = form.siteimg.data
            if site_file and allowed_file(site_file.filename):
                site_filename = secure_filename(str(uuid.uuid4()) + site_file.filename)
                site_file.save(os.path.join(app.config['UPLOAD_FOLDER'], site_filename))
            else:
                site_filename = ""
            f = Report(bimimg_filename = bim_filename, siteimg_filename = site_filename, site_caption = form.site_caption.data, date = form.date.data, note = form.note.data, summary = form.summary.data)
            db.session.add(f)
            db.session.commit()
            return redirect(url_for('report_waterproofing'))
        elif request.form['action'] == 'Edit Previous':
            id_object = form.edit_date.data
            r = Report.query.get(id_object.id)
            id = r.id
            return redirect(url_for('edit_waterproofing', id=id))
    elif request.method == 'GET':
        return render_template('create_waterproofing.html', form=form, data_type="Weekly Report", action="Add a")
示例#22
0
def upload():

    if request.method == 'POST':
        # try:
        pid = request.form["pid"]
        file_synonym = request.files["file1"]
        file_knowledge = request.files["file2"]

        print(file_synonym.filename)
        print(file_knowledge.filename)
        print(">>>>>>>")
        basepath = os.path.dirname(__file__)
        upload_path_synonyms = os.path.join(basepath, "load_file", "synonyms",
                                            secure_filename(file_synonym.filename))
        upload_path_knowledge = os.path.join(basepath, "load_file", "knowledge",
                                            secure_filename(file_knowledge.filename))
        upload_path_knowledge = os.path.join('/home/xp/work/upload_file/load_file/knowledge', file_knowledge.filename)
        print('------------', upload_path_synonyms)
        print('------------', upload_path_knowledge)
        file_synonym.save(upload_path_synonyms)
        file_knowledge.save(upload_path_knowledge)
        redirect(url_for('upload',file_name=file_synonym.filename))
        redirect(url_for('upload', file_name=file_knowledge.filename))
        print("保存成功")

        # except Exception as e:
        #     print("失败",e)
    # print(">>>MMMM")
    return render_template('index.html')
示例#23
0
def upload_game_file(platform_id):
	"""Upload a game rom file to the platforms rom folder."""

	try:
		f = request.files['file']
		platform = models.Platform.query.get_or_404(platform_id)

		file_type = os.path.splitext(utils.secure_filename(f.filename))[1][1:]
		filename_noExtension = os.path.splitext(utils.secure_filename(f.filename))[0]
		full_filename = utils.secure_filename(f.filename)

		if file_type == platform.extension:  # is this the file type we are using for roms?
			if not os.path.exists(platform.roms_path):
				os.makedirs(platform.roms_path)

			f.save(os.path.join(platform.roms_path, full_filename))
			game_id = ScanRoms.addGameRom(str(platform.id), filename_noExtension)

			return jsonify(result='OK', file_name=filename_noExtension, file_type=file_type, game_id=game_id)

		else:
			return jsonify(result='ERROR', file_name=filename_noExtension, file_type=file_type,
						   msg='Wrong file extension for platforms settings. Received ' + file_type + ' should be ' + platform.extension)

	except Exception, e:
		return jsonify(result='ERROR', file_name=filename_noExtension, file_type=file_type, msg=str(e))
示例#24
0
def upload_site_images():
    if request.files and len(request.files) > 0 and request.files['file']:
        file_body = request.files['file']
        if allowed_files(secure_filename(file_body.filename)):
            filename = secure_filename(str(uuid4()) + "." + file_body.filename.split('.')[1])
            file_body.save(os.path.join(upload_folder, filename))
            return json.dumps({"status": "success", "id": filename, "filename": filename})
    return '', 404
示例#25
0
文件: run.py 项目: dongweiming/aiglos
def section(chapter, section):
    chapter = secure_filename(chapter)
    section = secure_filename(section)
    path = os.path.join(BOOK_DIR, chapter, section)
    with codecs.open(path, 'r', 'utf-8') as f:
        content = f.read()
    content = Markup(markdown(content))
    return render_template('page.html', **locals())
示例#26
0
def save_mp3():
    newfile = request.files['newmp3']
    filename = secure_filename(newfile.filename)
    ## Bijzondere situatie: een geloade soundfile kan niet worden overschreven
    if currenttrack == filename:
        filename = "_" + secure_filename(newfile.filename)
    logger.info("Saving new sound file " + filename)
    newfile.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
    return redirect("/")
示例#27
0
def test_secure_filename():
    assert utils.secure_filename("My cool movie.mov") == "My_cool_movie.mov"
    assert utils.secure_filename("../../../etc/passwd") == "etc_passwd"
    assert (
        utils.secure_filename(u"i contain cool \xfcml\xe4uts.txt")
        == "i_contain_cool_umlauts.txt"
    )
    assert utils.secure_filename("__filename__") == "filename"
    assert utils.secure_filename("foo$&^*)bar") == "foobar"
示例#28
0
def upload_video():
    if current_user.is_anonymous():
        return '{"state":"fail"}'
    postBody = request.form
    live = postBody.get("live", "0") == "0"
    if live:
        # video
        try:
            UPLOAD_FOLDER = "youmu/static/uploads/videos/"
            ALLOWED_MIMETYPES = ("video/mp4", "video/x-matroska", "audio/mpeg")
            f = request.files["video"]
            ascii_name = f.filename.encode("ascii", "xmlcharrefreplace")
            fname = mktemp(suffix='_', prefix='u', dir=UPLOAD_FOLDER) + secure_filename(ascii_name)
            f.save(fname)
            mime_type = mimetypes.guess_type(fname)[0]
            if mime_type not in ALLOWED_MIMETYPES:
                os.remove(fname)
                return json.dumps({"state":"fail", "content":"wrong mime type"}, ensure_ascii = False)
            fname = str(fname)
            fname = fname[fname.find("/"):]
            media_type = "audio" if mime_type.find("mp3") != -1 else "video"
        except:
            return json.dumps({"state":"fail", "content":"video/audio upload failed"}, ensure_ascii = False)
        url = ""
    else:
        media_type = "live"
        url = postBody.get("rtmp", "")
        fname = ""
    # cover
    try:
        UPLOAD_FOLDER = "youmu/static/uploads/images/"
        ALLOWED_MIMETYPES = ("image/png", "image/jpeg", "image/jpg", "image/bmp")
        f = request.files["cover"]
        ascii_name = f.filename.encode("ascii", "xmlcharrefreplace")
        pname = mktemp(suffix='_', prefix='u', dir=UPLOAD_FOLDER) + secure_filename(ascii_name)
        f.save(pname)
        if mimetypes.guess_type(pname)[0] not in ALLOWED_MIMETYPES:
            os.remove(pname)
            return json.dumps({"state":"fail", "content":"wrong mime type"}, ensure_ascii = False)
        pname = str(pname)
        pname = pname[pname.find("/"):]
    except:
        pname = ""
    # other information
    category = postBody.get("category", "")
    if not category in VideoService.get_categories(): category = ""
    obj = Video(owner_id = current_user.id,
        title = postBody["title"],
        upload_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())),
        cover = pname,
        category = category,
        description = postBody["description"],
        media_type = media_type,
        url = url)
    VideoService.insert_video(obj, fname)
    return json.dumps({"state":"success"}, ensure_ascii = False)
def index():
    form = ProcessForm(request.form)

    # If there are form errors it will not pass form.validate() and
    # will fall through to the "GET" handler, passing along the errors
    # to the template.
    # This means we don't have to check the presence of (most) options
    if request.method == 'POST' and form.validate():
        schema_language = request.form.get('schema_language', None)
        source_option = request.form.get('source_options', None)
        canonicalized_source = request.files.get('source_canonical_file', None)
        customization_option = request.form.get('customization_options', None)
        local_customization = request.files.get('local_customization_file', None)
        verbose_output = request.form.get('verbose_output', None)

        uploaded_customization = None
        # at this point the form will have been checked to make sure the local customization
        # file is present if the local customization option is selected,
        # but we'll do another sanity check here just to make sure.
        if customization_option == 'z-local-customization' and local_customization is not None:
            tmpdir = tempfile.mkdtemp()
            filename = secure_filename(local_customization.filename)
            uploaded_customization = os.path.join(tmpdir, filename)
            local_customization.save(uploaded_customization)

        uploaded_source = None
        if source_option == 'local-source' and canonicalized_source is not None:
            tmpdir = tempfile.mkdtemp()
            filename = secure_filename(canonicalized_source.filename)
            uploaded_source = os.path.join(tmpdir, filename)
            canonicalized_source.save(uploaded_source)

        verbose = False
        if verbose_output == "y":
            verbose = True

        res = None
        res = package_files.apply_async(args=[schema_language, source_option, customization_option],
                                        kwargs={"uploaded_customization": uploaded_customization,
                                                "uploaded_source": uploaded_source,
                                                "verbose": verbose})

        return redirect(url_for('process_and_download') + "?cid=" + str(res))

    latest_git_revisions = None
    with open(os.path.join(app.root_path, 'info.json'), 'r') as info:
        js = json.load(info)
        latest_git_revisions = js.get('mei_git_revisions', None)
        tei_stylesheets_version = js.get('tei_stylesheets_version', None)

    d = {
        'latest_revisions': latest_git_revisions,
        'tei_stylesheets_version': tei_stylesheets_version,
    }

    return render_template("index.html", form=form, **d)
示例#30
0
 def _path_to_image(self, name, extension, mode=None, size=None):
     if mode:
         filename = secure_filename('%s-%dx%d.%s' % (mode, size[0], size[1], extension))
         directory = self._manipulated_directory(name, extension)
     else:
         filename = secure_filename(name + '.' + extension)
         directory = self._image_dir
     if not op.isdir(directory):
         os.makedirs(directory)
     return safe_join(directory, filename)
示例#31
0
def submit_start(request):
    """
    First view for submitting a file.
    """
    submit_form = submit_forms.SubmitStartForm(request.POST)

    if request.method == 'POST' and submit_form.validate():
        if not (request.POST.has_key('file')
                and isinstance(request.POST['file'], FieldStorage)
                and request.POST['file'].file):
            submit_form.file.errors.append(
                u'You must provide a file.')
        elif not security.check_filetype(request.POST['file']):
            submit_form.file.errors.append(
                u'The file doesn\'t seem to be an image!')
        else:
            filename = request.POST['file'].filename

            # create entry and save in database
            entry = request.db.MediaEntry()
            entry['title'] = (
                request.POST['title']
                or unicode(splitext(filename)[0]))

            entry['description'] = request.POST.get('description')
            entry['description_html'] = cleaned_markdown_conversion(
                entry['description'])
            
            entry['media_type'] = u'image' # heh
            entry['uploader'] = request.user['_id']

            # Save, just so we can get the entry id for the sake of using
            # it to generate the file path
            entry.save(validate=False)

            # Generate a slug from the title
            entry.generate_slug()

            # Now store generate the queueing related filename
            queue_filepath = request.app.queue_store.get_unique_filepath(
                ['media_entries',
                 unicode(entry['_id']),
                 secure_filename(filename)])

            # queue appropriately
            queue_file = request.app.queue_store.get_file(
                queue_filepath, 'wb')

            with queue_file:
                queue_file.write(request.POST['file'].file.read())

            # Add queued filename to the entry
            entry['queued_media_file'] = queue_filepath
            entry.save(validate=True)

            # queue it for processing
            process_media_initial.delay(unicode(entry['_id']))

            return redirect(request, "mediagoblin.submit.success")

    return render_to_response(
        request,
        'mediagoblin/submit/start.html',
        {'submit_form': submit_form})
示例#32
0
 def get_basename(self, filename):
     return lowercase_ext(secure_filename(filename))
示例#33
0
def namegen_filename(obj, file_data):
    """
        Generate secure filename for uploaded file.
    """
    return secure_filename(file_data.filename)
示例#34
0
def update_tool_submit(token):
    """
  Update tool page, takes care of the form submission
  """
    payload = verify_email_token(token)
    if not payload:
        flash('Link expired, try again')
        return redirect(url_for('index'))

    paper = Paper.query.get(payload['paper_id'])
    form = toolUpdate()
    if form.validate_on_submit():
        paper.tool_name = form.toolname.data
        paper.paper_name = form.papername.data
        paper.author_name = form.authorname.data
        paper.author_email = form.authoremail.data
        paper.description = form.description.data
        paper.bibtex = form.bibtex.data
        paper.category = form.category.data
        paper.link_to_pdf = form.linktopdf.data
        paper.link_to_demo = form.linktodemo.data
        paper.link_to_tool_webpage = form.linktotoolwebpage.data
        paper.link_to_archive = form.linktoarchive.data
        paper.year = form.year.data
        paper.conference = form.conference.data

        paper.tags = []

        for tag in form.tags.data.split(","):
            tag_obj = db.session.query(Tag).filter(
                Tag.tagname == tag.strip()).first()
            if tag_obj is None:
                tag_obj = Tag(tagname=tag.strip())
            paper.tags.append(tag_obj)

        db.session.flush()
        db.session.commit()

        filenames = []
        fileurls = []
        filetypes = form.file_types.data.split(',')

        for file in form.all_files.data:
            if not isinstance(file, str):
                filenames.append(secure_filename(file.filename))

                filename = '{}/{}'.format(paper.id,
                                          secure_filename(file.filename))
                file.filename = filename  # updating the name with paper.id for easy access

                bucket_location = s3.get_bucket_location(
                    Bucket=app.config['S3_BUCKET'])
                s3_url = "https://s3.{0}.amazonaws.com/{1}/{2}".format(
                    bucket_location['LocationConstraint'],
                    app.config['S3_BUCKET'], filename)

                upload_file_to_s3(s3, file, app.config['S3_BUCKET'])
                fileurls.append(s3_url)

        # print("Uploaded files below for paper: {}".format(paper.id))
        # print(fileurls)

        existing_files = [x.filename for x in paper.files]

        for filename, fileurl, filetype in zip(filenames, fileurls, filetypes):
            if filename not in existing_files:
                paper.files.append(
                    File(filename=filename, filetype=filetype,
                         fileurl=fileurl))

        db.session.flush()
        db.session.commit()

        flash('Tool information update successfully')
        return render_template('index.html')

    return render_template('tool_update.html',
                           title="Update your tool here",
                           form=form)
示例#35
0
def delete_user_picture(filename):
    delete_file = secure_filename(filename)
    os.remove(os.path.join(app.config['USER_UPLOAD_FOLDER'], delete_file))
    return
示例#36
0
def add_admin():
    res = {}

    file = request.files['image']
    if 'firstname' in request.form and 'lastname' in request.form and 'email' in request.form and 'password' in request.form:

        firstname = request.form['firstname']
        lastname = request.form['lastname']
        email = request.form['email']
        password = request.form['password']
        position = request.form['position']
        phone = request.form['phone']
        role = request.form['role']
        user_type = request.form['user_type']

        print(request.files['image'])

        get_filename = secure_filename(file.filename)
        filename, file_extension = os.path.splitext(get_filename)

        today = datetime.date.today()
        today = str(today)
        #encodedBytes = base64.b64encode(today.encode("utf-8"))
        #encodedStr = str(encodedBytes, "utf-8")

        filename = today + '-' + firstname + file_extension

        filename = filename.replace(' ', '-').lower()

        print(filename)

    else:
        if not 'image' in request.files: res["error"] = "No Image"

        if not allowed_file(file.filename):
            res["error"] = "File type not supported"

        return jsonify({"data": res})

    filename = os.path.join(app.config['UPLOAD_FOLDER'], filename)

    #Cropp image
    #img = Image.open(filename)
    #area =(200, 100, 700,400)
    #new_sizeed_file = img.crop(area)
    #new_sizeed_file.show(new_sizeed_file)

    print(filename)

    if not os.path.exists(UPLOAD_FOLDER):
        os.makedirs(UPLOAD_FOLDER)

    temp_file = os.path.join(app.config['UPLOAD_FOLDER'], "temp.jpg")

    file.save(temp_file)

    storage.child(filename).put(temp_file)

    # Get image url from firebase
    img_url = storage.child(filename).get_url(None)

    #res["msg"] = "Valid_Image"
    shutil.copy(temp_file, filename)
    file = request.files['image']

    res["media"] = filename

    print(request.files)

    req_data = request.get_json()
    print(req_data)

    hashed_password = generate_password_hash(request['password'],
                                             method='sha256')
    data = {
        'firstname': firstname,
        'lastname': lastname,
        'avatar_thumbnail': img_url,
        'email': email,
        'password': hashed_password,
        'position': position,
        'phone': phone,
        'role': role,
    }

    data['_id'] = str(ObjectId())
    x = database["admins"].insert_one(data)

    #memberID = request.form['memberID']
    #database['users'].update({'_id': memberID}, {"$set": {'profile_bg':profile_bg}})

    os.remove(temp_file)

    return jsonify({"data": res})
def compression():
	if request.method == 'POST':
	
        # check if the post request has the file part
		if 'file' not in request.files:
			flash('No file part')
			return redirect(request.url)

		#### check for file name validation
		file = request.files['file']
		if file.filename == '':
			flash('No file selected for uploading')
			return redirect(request.url)

		### validation on file extention
		if file and allowed_file(file.filename):

			##### clean up the environment
			os.system('rm -f /myAllProjects/new_bigdata/extracted/*')
			os.system('rm -f /myAllProjects/new_bigdata/file_chunk/*')
			os.system('rm -f /myAllProjects/new_bigdata/uploaded/*')
			os.system('rm -f /myAllProjects/new_bigdata/decompressed/*')
			os.system('rm -f /myAllProjects/new_bigdata/selective.zip')
			filename = secure_filename(file.filename)
			file.save('/myAllProjects/new_bigdata/'+filename)
			
			df=pd.read_csv('/myAllProjects/new_bigdata/'+filename)
			df1=df.drop_duplicates(subset=None, inplace=False)
			df1.to_csv('/myAllProjects/new_bigdata/'+filename)
			csvfilename = open('/myAllProjects/new_bigdata/'+filename, 'r').readlines()
			filesize=60000
			#header = csvfilename[0]
			csvfilename.pop(0) 
			count=0
			l=[]
			for i in range(len(csvfilename)):

				if i % filesize == 0:
					
					### folder name
					folder=app.config['UPLOAD_FOLDER']
					
					### file chunk created
					write_file = csvfilename[i:i+filesize]
					#if(i==0):
					#	write_file.insert(0,header)

					### filename which is to be compress is written inside number.txt
					open(folder+str(count)+filename, 'w+').writelines(write_file)
					file=open('/myAllProjects/new_bigdata/number.txt','w+')
					file.write(str(count)+filename)
					file.close()
					
					### dockerfile created and run in background
					check=subprocess.getoutput('docker ps -a | grep container{}'.format(count))
					if(check!=''):
						os.system('docker rm -f container{}'.format(count))
					os.system('docker build -t "dockerfile" /myAllProjects/new_bigdata/')
					os.system('docker run -dit -v /myAllProjects/new_bigdata/file_chunk/:/usr/local/bigdata/file -v /myAllProjects/new_bigdata/uploaded/:/usr/local/bigdata/compressed/ --name container{} dockerfile'.format(count))

					count += 1
			while True:
				#### check for existence of docker container
				output=subprocess.getoutput('docker ps | wc -l')
				if(output=='2'):
					for i in range(count):
						ps=subprocess.getoutput('docker ps | grep container{}'.format(i))
						psa=subprocess.getoutput('docker ps -a| grep container{}'.format(i))
						if(ps=='' and psa!=''):
							os.system('docker rm -f container{}'.format(str(i)))
					gzip()
					return send_file('/myAllProjects/new_bigdata/selective.zip',attachment_filename='selective.zip')	
		
			flash('File successfully uploaded')
			return redirect('/')
		else:
			flash('Allowed file types are txt, pdf, png, jpg, jpeg, gif')
			return redirect(request.url)
def img_u():
    if "type" in session:
        if request.method == 'POST':
            f = request.files['pic']
            f.save(os.path.join(img_path, secure_filename(f.filename)))
            return "done"
示例#39
0
def generate():
    uploaded_file = request.files['file']
    filename = secure_filename(uploaded_file.filename)
    if filename != '':
        file_ext = os.path.splitext(filename)[1]
        if file_ext not in app.config['UPLOAD_EXTENSIONS']:
            return "Invalid file", 400

        data = json.loads(request.form['data'])

        file_path = os.path.join('documentation/' + data['pilot'] + '/',
                                 filename)
        uploaded_file.save(file_path)

        # Instantiate PathwayGeneration object
        if 'model' in data:
            pgr = PathwayGenerator(
                file_path=file_path,
                pilot=data['pilot'],
                service=data['service'],
                use_cuda=True,
                cuda_device=0,
                annotation_model=data['model'],
                section_split_model=
                'section_split/models/training_unfolding_structure-2020-12-22_11-07-07_distilroberta-base'
            )
        else:
            pgr = PathwayGenerator(
                file_path=file_path,
                pilot=data['pilot'],
                service=data['service'],
                use_cuda=True,
                cuda_device=0,
                section_split_model=
                'section_split/models/training_unfolding_structure-2020-12-22_11-07-07_distilroberta-base'
            )

        # Check for projects
        generation_project = get_project_by_name('ER ' +
                                                 pilots_legend[data['pilot']] +
                                                 ' Pathways')
        annotation_project = get_project_by_name('ER ' +
                                                 pilots_legend[data['pilot']] +
                                                 ' Annotated Documents')
        #app.config['logger'].log()
        # Check if document already exists: if so, return annotations. Otherwise, create a new one
        document_annotation = get_document(pgr.annotation_metadata,
                                           annotation_project['id'])
        document_generation = get_document(pgr.generation_metadata,
                                           generation_project['id'])
        if document_generation:
            #document_where = get_document(pgr.generation_metadata['where'], generation_project['id'])
            #document_when = get_document(pgr.generation_metadata['when'], generation_project['id'])
            #document_how = get_document(pgr.generation_metadata['how'], generation_project['id'])

            document_generation = sorted(
                document_generation,
                key=lambda x: int("".join(
                    [i for i in x['text'] if i.isdigit()])))
            #document_generation = sorted(document_generation, key = lambda x: x['text'])
            return refactor_export_generations(document_generation)

        # Check if document already exists: if so, return annotations. Otherwise, create a new one
        if document_annotation:
            annotations = doccano_client.get_annotation_list(
                annotation_project['id'], document_annotation[0]['id'])

            pgr.ner_dict = doccano_to_dict_format(annotations,
                                                  document_annotation[0],
                                                  annotation_project['id'])

        converted_file = pgr.do_convert()
        #app.config['logger'].log()
        ner_dict = pgr.do_annotate(pgr.to_list())
        doccano_dict, ner_path = pgr.export_annotation_to_doccano()
        try:
            doccano_client.post_doc_upload(project_id=annotation_project['id'],
                                           file_format='json',
                                           file_name=ner_path)
        except json.decoder.JSONDecodeError:
            pass
        sections = pgr.do_split()
        full_ner_dict = {}
        count = 1
        for section in sections:
            pgr.annotation_model.reset_preprocesser()
            ner_dict = pgr.do_annotate(section)
            pathway = pgr.do_generate()
            label = 'Step' + str(count)
            full_ner_dict[label] = pathway
            count = count + 1
        pathway_dict, pathway_path = pgr.export_generation_to_doccano(
            full_ner_dict)
        #print(pathway_dict)
        #app.config['logger'].log()

        #pathway = pgr.do_generate()
        #app.config['logger'].log()

        try:
            #print('Uploading documents')
            if count < 50:
                doccano_client.post_doc_upload(
                    project_id=generation_project['id'],
                    file_format='json',
                    file_name=pathway_path)
        except json.decoder.JSONDecodeError:
            pass

        return pathway_dict

    return 'NOK', 400
示例#40
0
def get_new_file_name(image_file):
    timestamp = datetime.datetime.utcnow().strftime("%s.%f")
    timestamped_filename = hashlib.md5(os.path.join(timestamp + '_' + image_file.filename).encode()).hexdigest() + '.'\
                           + image_file.filename.rsplit('.', 1)[1].lower()
    return secure_filename(timestamped_filename)
def account():
    form = UpdateAccountForm(request.form)
    if request.method == 'GET':
        if session.get('logged_in') is None:
            session['logged_in'] = False

        if session['logged_in']:
            conn = mysql.connect()
            cur = conn.cursor()

            cur.execute("SELECT username,email_id,dob,address,phone,work,education,about,profile_picture FROM user WHERE email_id = %s", (session['logged_user_id']))
            result = cur.fetchall()
            results = [list(i) for i in result]

            for item in results:
                form.name.data = item[0]
                form.email.data = item[1]
                form.dob.data = item[2]
                form.address.data = item[3]
                form.phone.data = item[4]
                form.work.data = item[5]
                form.education.data = item[6]
                form.details.data = item[7]
                form.picture.data = item[8]
                if item[8]:
                    full_profilepic_path = "..\\static\\profile_pics\\" + item[8]
                else:
                    full_profilepic_path = "..\\static\\profile_pics\\default.jpg"
                # print(full_profilepic_path)
            return render_template('account.html', title='My Profile', form=form, full_profilepic_path=full_profilepic_path)
        else:
            return render_template('index.html', title='Home')

    if request.method == 'POST':
        # print("YAY")
        name = form.name.data
        dob = form.dob.data
        address = form.address.data
        phone = form.phone.data
        work = form.work.data
        education = form.education.data
        details = form.details.data

        conn = mysql.connect()
        cur = conn.cursor()

        cur.execute("SELECT profile_picture FROM user WHERE email_id = %s", (session['logged_user_id']))
        results = cur.fetchall()
        for item in results:
            form.picture.data = item[0]
        conn.commit()
        cur.close()
        if form.picture.data is None:
            full_profilepic_path = "..\\static\\profile_pics\\default.jpg"
            filename = "default.jpg"
        else:
            full_profilepic_path = "..\\static\\profile_pics\\" + form.picture.data
            filename = form.picture.data

        # uploading pic
        if 'file' not in request.files:
            flash('No file part')
        else:
            file = request.files['file']

            # if user does not select file, browser also
            # submit a empty part without filename
            if file.filename == '':
                flash('No selected file')
            if file and allowed_file(file.filename):
                filename = secure_filename(file.filename)
                #print(os.path.join(app.config['UPLOAD_FOLDER'], filename))
                file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
                full_profilepic_path = "..\\static\\profile_pics\\" + filename

        conn = mysql.connect()
        cur = conn.cursor()
        if len(form.password.data) > 0:
            password = form.password.data
            cur.execute("""UPDATE user SET username = %s,password = %s, dob = %s, address = %s, phone = %s, work = %s, education = %s, about = %s, profile_picture = %s WHERE email_id = %s""", (name, password, dob, address, phone, work, education, details, filename, session['logged_user_id']))
            form.password.data = ""
        else:
            cur.execute("""UPDATE user SET username = %s, dob = %s, address = %s, phone = %s, work = %s, education = %s, about = %s, profile_picture = %s WHERE email_id = %s""", (name, dob, address, phone, work, education, details, filename, session['logged_user_id']))
        conn.commit()
        cur.close()
    return render_template('account.html', title='Account', form=form, full_profilepic_path=full_profilepic_path)
示例#42
0
def uploader():
    if ('user' in session and session['user'] == params['admin_user']):
        if (request.method == 'POST'):
            f= request.files['file1']
            f.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename) ))
            return "Uploaded successfully"
示例#43
0
def upload():
    if request.method == 'POST':
        # Get the file from post request
        f = request.files['file']

        # Save the file to ./uploads
        basepath = os.path.dirname(__file__)
        file_path = os.path.join(basepath, 'uploads',
                                 secure_filename(f.filename))
        f.save(file_path)

        # Make prediction
        output_class = [
            "batteries", "cloth", "e-waste", "glass", "light bulbs",
            "metallic", "organic", "paper", "plastic"
        ]

        preds = model_predict(file_path, model)
        print(preds)

        pred_class = output_class[np.argmax(preds)]
        pred_class_percent = round(np.max(preds) * 100, 2)

        result = 'It is ' + pred_class + ' waste'  # Convert to string
        pred_class = ' with ' + str(pred_class_percent) + '% confidence'

        #k-nn for recommending
        filelist.sort()
        featurelist = []
        for i, imagepath in enumerate(filelist):
            print("    Status: %s / %s" % (i, len(filelist)), end="\r")
            img = image.load_img(imagepath, target_size=(224, 224))
            img_data = image.img_to_array(img)
            img_data = np.expand_dims(img_data, axis=0)
            img_data = preprocess_input(img_data)
            features = np.array(model.predict(img_data))
            featurelist.append(features.flatten())
        nei_clf = NearestNeighbors(metric="euclidean")
        nei_clf.fit(featurelist)
        code = model_predict(file_path, model)
        (distances, ), (idx, ) = nei_clf.kneighbors(code, n_neighbors=3)

        #all images are loaded as np arrays
        images = []
        labels = []
        j = 1
        for i, image_path in enumerate(filelist):
            images.append(load_data(image_path))
        images = np.asarray(
            images
        )  # all of the images are converted to np array of (1360,224,224,3)

        print(distances, images[idx])
        print(images[idx].shape)

        final_result = result + pred_class
        image_save = Image.fromarray(
            (np.array(images[0]) * 255).astype(np.uint8))
        #image_save = Image.fromarray(images[idx], "RGB")
        image_save.save('out.jpg')
        image_output = os.path.join(basepath, 'out.jpg')
        immg = '<img src="' + image_output + '" style="height: 132px; width: 132px;">'
        #return render_template('index.html', filename=image_output)
        return final_result
    return None
示例#44
0
def edit_covenants():
    """Render covenant editing form."""

    allowed_extensions = ['jpg', 'png']

    form = EditInstitutionsWithCovenantsForm()

    pfactory = PosGraduationFactory(current_user.pg_initials)
    dao = pfactory.integrations_infos_dao()
    json = pfactory.integrations_infos_dao().find_one()
    json = dict(json)
    json = dumps(json)

    if form.validate_on_submit() and form.create.data:
        index = str(form.index.data)
        if form.logo.data and allowedFile(form.logo.data.filename,
                                          allowed_extensions):
            photo = form.logo.data
            path = os.path.normpath("static/assets")
            filename = secure_filename(photo.filename)
            if filename.count('.') > 1:
                return redirect(
                    url_for(
                        'admin.edit_covenants',
                        integrations=json,
                        success_msg=
                        'Nome da logo contem mais de um . por favor corrija isso'
                    ))
            name, extension = filename.split('.')
            logoFile = 'logo-' + form.initials.data.lower() + '.' + extension
            logo = uploadFiles(photo, path, logoFile)
            new_covenant = {
                'name': form.name.data,
                'initials': form.initials.data.upper(),
                'logoFile': logo
            }

            dao.find_one_and_update(
                None,
                {'$set': {
                    'institutionsWithCovenant.' + index: new_covenant
                }})
        else:
            dao.find_one_and_update(
                None, {
                    '$set': {
                        'institutionsWithCovenant.' + index + '.initials':
                        form.initials.data.upper()
                    }
                })
            dao.find_one_and_update(
                None, {
                    '$set': {
                        'institutionsWithCovenant.' + index + '.name':
                        form.name.data
                    }
                })

        return redirect(
            url_for('admin.edit_covenants',
                    integrations=json,
                    success_msg='Convênio editado com sucesso.'))

    return render_template('admin/edit_covenants.html',
                           form=form,
                           integrations=json,
                           success_msg=request.args.get('success_msg'))
def upload_file():    
    if request.form.getlist('obs'):
        ff=request.form.getlist('obs')
        if len(ff) == 0:
            return render_template("objres.html", name = urls,data=fin,user="******")
        for objects in ff:
            hashtags=HashSearch(objects)
            ss=[]
            single=[]
            for w in hashtags:
                if detect(w)=="en":
                    ss.append(w)
            for q in ss:
                if q not in single:
                    single.append(q)
            allt.append(single)
        str1 = ""
        uu=""
        for tt in urls:
            uu=uu+" "+tt+", "
        pic_urls=str.format(uu)
        for ele in fin: 
            for e in ele:
                str1=str1+" #"+e+" "
        dis=str.format(str1)
        allhash=""
        for r in allt:
            for t in r:
                allhash=allhash+" #"+e+" "
        oos=""
        for ty in res:
            oos=oos+" "+ty+", "
        data = {
            u'user': "******",
            u'time': dt_string,
            u'hashtag_g': dis,
            u'hashtag_obj_twitter':allhash,
            u'obj_identified':oos,
            u'img_urls':pic_urls
            }
        mydata.set(data)        
        return render_template("result.html", name = urls,data=fin,hashes=allt,user="******")

        
    if request.method == 'POST':
        fin.clear()
        res.clear()
        urls.clear()
        allt.clear()
        fff.clear()
        tts.clear()
        files = request.files.getlist('files[]')
        r=request.form.get('radio')       
        print(r)
        if request.form.get('radio') == '1':
           
            for file in files:
                if file and allowed_file(file.filename):
                    filename = secure_filename(file.filename)
                    fff.append(filename)
                    file.save(os.getcwd()+"/static/uploads/"+filename)
                    p=os.getcwd()+"/static/uploads/"+filename
                    global bucket_name
                    ur=upload_blob(bucket_name,p,filename)
                    urls.append(ur)
                    with io.open(p, 'rb') as image_file:
                        content = image_file.read()
                    image = vision.types.Image(content=content)
                    response = client.label_detection(image=image)
                    labels = response.label_annotations
                    print('Labels:')
                    for label in labels:
                        temp=label.description
                        y=temp.replace(" ","")
                        tts.append(y)
                    print(tts)
                    fin.append(tts)
                    vals=run_detector(detector, p)                  
                    for o in vals:
                        if o not in res:
                            res.append(o)

        else:
            for file in files:
                if file and allowed_file(file.filename):
                    filename = secure_filename(file.filename)
                    fff.append(filename)
                    file.save(os.getcwd()+"/static/uploads/"+filename)
                    p=os.getcwd()+"/static/uploads/"+filename
                    global bucket_name
                    ur=upload_blob(bucket_name,p,filename)
                    urls.append(ur)
                    with io.open(p, 'rb') as image_file:
                        content = image_file.read()
                    image = vision.types.Image(content=content)
                    response = client.label_detection(image=image)
                    labels = response.label_annotations
                    print('Labels:')
                    for label in labels:
                        temp=label.description
                        y=temp.replace(" ","")
                        tts.append(y)
                    print(tts)
                    fin.append(tts)
            str1 = ""
            uu=""
            for tt in urls:
                uu=uu+" "+tt+", "
            pic_urls=str.format(uu)
            for ele in fin: 
                for e in ele:
                    str1=str1+" #"+e+" "
            dis=str.format(str1)
            data = {
                u'user': "******",
                u'time': dt_string,
                u'hashtag_g': dis, 
                u'pic_urls':pic_urls           
                }
            mydata.set(data)
            return render_template("result.html", name = urls,f=fin,user="******")
        return render_template("result.html", name = urls,f=res,user="******")
示例#46
0
def annotate():

    uploaded_file = request.files['file']
    filename = secure_filename(uploaded_file.filename)
    if filename != '':
        file_ext = os.path.splitext(filename)[1]
        if file_ext not in app.config['UPLOAD_EXTENSIONS']:
            return "Invalid file", 400

        app.config['logger'].log({'file': 'test'})

        data = json.loads(request.form['data'])

        file_path = os.path.join('documentation/' + data['pilot'] + '/',
                                 filename)
        uploaded_file.save(file_path)

        # Instantiate PathwayGeneration object
        if 'model' in data:
            pgr = PathwayGenerator(file_path=file_path,
                                   pilot=data['pilot'],
                                   service=data['service'],
                                   use_cuda=True,
                                   cuda_device=0,
                                   annotation_model=data['model'])
        else:
            pgr = PathwayGenerator(file_path=file_path,
                                   pilot=data['pilot'],
                                   service=data['service'],
                                   use_cuda=True,
                                   cuda_device=0)

        # Check for annotation project
        project = get_project_by_name('ER ' + pilots_legend[data['pilot']] +
                                      ' Annotated Documents')
        #app.config['logger'].log()

        # Check if document already exists: if so, return annotations. Otherwise, create a new one
        document = get_document(pgr.annotation_metadata, project['id'])
        if document:
            return refactor_export_annotations(document[0], project['id'])

        converted_file = pgr.do_convert()
        #app.config['logger'].log()

        ner_dict = pgr.do_annotate(pgr.to_list())
        #app.config['logger'].log()
        print(ner_dict)

        doccano_dict, ner_path = pgr.export_annotation_to_doccano()
        #app.config['logger'].log()

        # WARNING: current issue of file upload/download Response -> https://github.com/doccano/doccano-client/issues/13
        try:
            doccano_client.post_doc_upload(project_id=project['id'],
                                           file_format='json',
                                           file_name=ner_path)
        except json.decoder.JSONDecodeError:
            pass

        return doccano_dict

    return 'NOK', 400
示例#47
0
def item_posting():

    # cursor = db.cursor()
    print(request.form)
    formsLen = len(request.form)
    images_path = []

    if 'lazyRegistration' in session:
        print('session file is: ', session_file)
        insertItemPost(session['item_name'], session['item_category'],
                       session['item_desc'], session['item_price'],
                       session['is_tradable'], session['item_images'],
                       session['sessionUser'], True)
        session_file.clear()
        session.pop('lazyRegistration')
        session.pop('lazyPage')
        print('Rediret from lazy login to home')
        # return render_template('home.html', sessionUser=session['sessionUser'], id=-1,categoryName="Catogory")
        return redirect("/")

    sessionUser = "" if 'sessionUser' not in session else session['sessionUser']
    # print("Session user", sessionUser)

    if request.method == "POST":
        if request.form:
            print("printing request form", request.form)

        if formsLen > 0:
            item_name = str(bleach.clean(request.form['item_title']))
            item_category = request.form['category']
            item_desc = str(bleach.clean(request.form['item_desc']))
            item_price = request.form['item_price']
            is_tradable = '0' if 'isTradable' not in request.form else request.form[
                'isTradable']  #str(1) if 'isTradable' in request.form else str(0)
            item_images = []
            if sessionUser == "":
                session['item_images'] = []

            # store image in separate folder as per category
            UPLOAD_FOLDER = 'static/images/' + item_category
            session['UPLOAD_FOLDER'] = UPLOAD_FOLDER

            for file in request.files.getlist('file'):
                if file.filename == '':
                    print('No file selected for uploading')
                else:
                    # session['item_image'].append(base64.b64encode(file.read()).decode('ascii'))
                    if sessionUser == "":
                        # session_file.append(file)

                        if file and allowed_file(file.filename):

                            filename = secure_filename(file.filename)

                    # unique filename
                        uuid_val = uuid.uuid1()
                        filename = str(uuid_val) + '.' + \
                            filename.rsplit('.', 1)[1].lower()
                        print(os.path.curdir)
                        file_path = os.path.join(session['UPLOAD_FOLDER'],
                                                 filename)
                        print("file path from item-posting post req is:",
                              file_path)
                        # file = open(file,"wr")
                        file.save(file_path)
                        session['item_images'].append(file_path)
                    else:
                        item_images.append(file)

            if sessionUser == "":
                session['lazyRegistration'] = True
                session['lazyPage'] = 'item-posting'
                session['item_name'] = item_name
                session['item_category'] = item_category
                session['item_desc'] = item_desc
                session['item_price'] = item_price
                session['is_tradable'] = is_tradable
                # session['item_userid'] =
                # session['item_images'] = None #item_images

                print("going to login?")
                return redirect("/login")

            else:
                # sessionUser = session['sessionUser']
                insertItemPost(item_name, item_category, item_desc, item_price,
                               is_tradable, item_images, sessionUser, False)

    if request.method == "GET":
        cursor = getCursor()[1]
        cursor.execute(query().fetchAllCategories())
        allCategories = cursor.fetchall()
        cursor.close()
        categories = [allCategories[i][0] for i in range(len(allCategories))]
        return render_template("item-posting.html", categories=categories)

    flash("Item Posted Successfully")

    return redirect('/')
def upload_file():
    if 'email' not in session:
        return render_template('please_login_first.html')
    else:
        if session['level'] == 0:
            return render_template('Users_cannot_publish_tasks.html')
        else:
            if request.method == 'POST':
                # check if the post request has the file part
                if 'file' not in request.files:
                    print('no file')
                    return redirect(request.url)
                file = request.files['file']
                # if user does not select file, browser also
                # submit a empty part without filename
                if file.filename == '':
                    print('no filename')
                    return redirect(request.url)
                if file and allowed_file(file.filename):
                    # Save file
                    filename = secure_filename(file.filename)
                    save_path = os.path.join(app.config['UPLOAD_FOLDER'],
                                             filename)
                    file.save(save_path)
                    # Unzip file to the 'EXTRACT_FOLDER/projectName_timeStamp'
                    zip_ref = zipfile.ZipFile(save_path, 'r')
                    final_path = os.path.join(
                        EXTRACT_FOLDER,
                        filename.split(".zip")[0] + "_" +
                        datetime.datetime.today().strftime('%Y-%m-%d_%H:%M:%S')
                    )
                    if not os.path.exists(final_path):
                        print(final_path)
                        os.makedirs(final_path)
                    zip_ref.extractall(final_path)
                    zip_ref.close()
                    # Only keep Json files
                    for filename in os.listdir(final_path):
                        if not os.path.join(final_path,
                                            filename).endswith(".json"):
                            if os.path.isdir(os.path.join(
                                    final_path, filename)):
                                shutil.rmtree(
                                    os.path.join(final_path, filename))
                            else:
                                os.remove(os.path.join(final_path, filename))
                        elif filename == "meta.json":
                            with open(os.path.join(final_path, filename)) as f:
                                meta = json.load(f)
                                sourcename = meta['projectName']
                                description = meta['description']
                                fault_level = meta['fault_level']
                            f.close()
                            os.remove(os.path.join(final_path, filename))
                    root_path = os.path.join(
                        EXTRACT_FOLDER, sourcename + "_" +
                        datetime.datetime.today().strftime('%Y-%m-%d_%H:%M:%S')
                    )
                    shutil.move(final_path, root_path)
                    admin_email = session['email']
                    c = init_cnx()
                    admin_id = c.get_adminid(email_addr=admin_email)
                    c.close()
                    result = {"code": 0}
                    c = init_cnx()
                    insert = c.insert_source(sourcename=sourcename,
                                             publisher=admin_id,
                                             description=description,
                                             publish_time=get_timestamp(),
                                             priority=1,
                                             ft_degree=fault_level)
                    c.close()
                    if insert == -1:
                        result = {
                            "code": 1,
                            "message": "Task insertion failed!"
                        }
                        return jsonify(result)
                    else:
                        c = init_cnx()
                        load = c.load_data(root_path=root_path,
                                           sourcename=sourcename)
                        c.close()
                        if load == 0:
                            result = {
                                "code": 1,
                                "message": "Data insertion failed!"
                            }
                            return jsonify(result)
                        else:
                            if session['level'] == 1:
                                return render_template('indexA1.html')
                            elif session['level'] == 2:
                                return render_template('indexAX.html')
    return render_template('publish.html')
示例#49
0
def muti_scan():
    search_hostlist = ''
    if request.method == 'POST':
        file = request.files['input_list']

        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            search_hostlist = str(filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

    scan_mode_list = [{
        'id': '1',
        'arg': '-sT',
        'name': 'TCP Connect Scan',
        'text': u'利用TCP協定,建立完整的3向交握連線後在進行掃描,雖然準確率比較高,但易留下紀錄。'
    }, {
        'id': '2',
        'arg': '-sS',
        'name': 'SYN Stealth Scan',
        'text': u'半開放掃描,若對方有開啟TCP端口就會回應封包,不會留下紀錄。'
    }, {
        'id': '3',
        'arg': '-sA',
        'name': 'ACK Scan ',
        'text': u'探測目標是否有防火牆阻擋。'
    }, {
        'id': '4',
        'arg': '-sM',
        'name': 'Mainmon Scan',
        'text': u'判斷那些端口是開啟狀態。'
    }, {
        'id': '5',
        'arg': '-sF',
        'name': 'FIN Scan',
        'text': u'正常連線型態,隱密性較高。'
    }, {
        'id': '6',
        'arg': '-sN',
        'name': 'NULL Scan',
        'text': u'類似-sF,但對Windows無作用。'
    }, {
        'id': '7',
        'arg': '-sX',
        'name': 'XMas Tree Scan',
        'text': u'利用送出三個控制封包探測目標主機,對Windows無作用。'
    }, {
        'id': '8',
        'arg': '-sU',
        'name': 'UDP Scan',
        'text': u'探測目標是否提供UDP服務。'
    }, {
        'id': '9',
        'arg': '-sO',
        'name': 'IP Protocol Scan',
        'text': u'偵測目標系統哪些IP協定。'
    }, {
        'id': '10',
        'arg': '-sP',
        'name': 'Ping Sweep',
        'text': u'單純Ping掃描。'
    }]
    ping_list = [{
        'id': '1',
        'arg': '-P0',
        'name': 'Don\'t Ping',
        'text': u'執行掃描前,不目標主機。'
    }, {
        'id': '2',
        'arg': '-PI',
        'name': 'ICMP Echo',
        'text': u'查詢目標是否正在運行。'
    }, {
        'id': '3',
        'arg': '-PE',
        'name': 'Use ICMP Ping Host',
        'text': u'利用ICMP封包Ping目標。'
    }, {
        'id': '4',
        'arg': '-PR',
        'name': 'ARP Ping',
        'text': u'只能使用同區段的區域網路。'
    }, {
        'id': '5',
        'arg': '-PS',
        'name': 'TCP SYN Ping',
        'text': u'發出TCP SYN封包,需指定端口,預設端口80。'
    }, {
        'id': '6',
        'arg': '-PA',
        'name': 'TCP ACK Ping',
        'text': u'發出TCP ACK封包,需指定端口,預設端口80。'
    }, {
        'id': '7',
        'arg': '-PU',
        'name': 'UDP Ping',
        'text': u'發出空的UDP封包,需指定端口,預設端口31338。'
    }]
    speed_list = [{
        'id': '1',
        'arg': '-T0',
        'name': 'Paranoid',
        'text': u'每五秒鐘發送一個封包。'
    }, {
        'id': '2',
        'arg': '-T1',
        'name': 'Sneaky',
        'text': u'每十五秒鐘發送一個封包。'
    }, {
        'id': '3',
        'arg': '-T2',
        'name': 'Polite',
        'text': u'每0.4秒鐘發送一個封包。'
    }, {
        'id': '4',
        'arg': '-T3',
        'name': 'Normal',
        'text': u'預設及不指定速度的掃描。'
    }, {
        'id': '5',
        'arg': '-T4',
        'name': 'Aggressive',
        'text': u'每十毫秒鐘發送一個封包。'
    }, {
        'id': '6',
        'arg': '-T5',
        'name': 'Insane',
        'text': u'每五毫秒鐘發送一個封包。'
    }]
    other_list = [
        {
            'id': '1',
            'arg': '-f',
            'name': 'Fragmentation',
            'text': u'發送碎片封包,資料長度為8byte,增加封包過濾器、防火牆與IDS的檢查難度。'
        },
        {
            'id': '2',
            'arg': '-6',
            'name': 'IPv6',
            'text': u'支援掃描IPv6TAT。'
        },
    ]
    task_list = mon.toybox.task_list.find()
    return render_template('muti_scan.html',
                           search_hostlist=search_hostlist,
                           scan_mode_list=scan_mode_list,
                           ping_list=ping_list,
                           speed_list=speed_list,
                           other_list=other_list,
                           task_list=task_list)
示例#50
0
def upload_file():
    if request.method == 'POST':
        # Removing Files
        # Pfiles = glob.glob('uploads/*')
        # for f in Pfiles:
        #     os.remove(f)

        # Pfiles2 = glob.glob('uploads2/*')
        # for f in Pfiles2:
        #     os.remove(f)
        req = request.form
        # check if the post request has the file part
        if req['form-name'] == 'form1':
            text_area = req['txtarea']
            percent = int(req['percent'])
            percent = percent / 100
            print(percent)
            sum_text = summarize(text_area, percent)
            return render_template('upload_file.html',
                                   in_text=text_area,
                                   sum_text=sum_text)
        if 'file' not in request.files:
            print('no file')
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit a empty part without filename
        if file.filename == '':
            print('no filename')
            return redirect(request.url)
        else:
            filename = secure_filename(file.filename)

            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

            text = " ".join((line for line in open(os.path.join(
                app.config['UPLOAD_FOLDER'], filename),
                                                   encoding='utf-8')))
            # os.remove(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            percent = int(req['percent'])
            percent = percent / 100
            print(text)
            text2 = summarize(text, percent)
            print("after summary.....")

            # file_path = app.config['UPLOAD_FOLDER2']
            print(filename)
            # filename= "SUM_" + filename
            # files = open(file_path+ sum_file, 'w')
            # files.write(text2)
            # files.close()
            with open(os.path.join(app.config['UPLOAD_FOLDER2'], filename),
                      'w') as f:
                f.write(text2)
            print("here is the filename : ", filename)

            print("saved file successfully")
            #send file name as parameter to downlad
            # file_path = os.path.join(app.config['UPLOAD_FOLDER2'], filename)
            # print("file for sent is ::: ")
            # return send_file(file_path, as_attachment=True, attachment_filename='')
            # file_path = os.path.join(app.config['UPLOAD_FOLDER2'], filename)

            return redirect('/downloadfile/' + filename)
    return render_template('upload_file.html',
                           in_text="Text area for input ...")
示例#51
0
def upload_file():
    # check if the post request has the file part
    if 'file' not in request.files:
        resp = jsonify({'message' : 'No file part in the request'})
        resp.status_code = 400
        return resp

    file = request.files['file']

    if file.filename == '':
        resp = jsonify({'message' : 'No file selected for uploading'})
        resp.status_code = 400
        return resp

    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        file_name= file.filename
        file_name = file_name.split(".")

        if file_name[1] == 'txt':
            metadata_txt = {}
            data_txt={}
            file_tmp = request.files['file'].read()
            file_tmp = file_tmp.decode("utf8")
            metadata_txt['file name']=file_name[0]
            file.seek(0, os.SEEK_END)
            file_length = file.tell()
            metadata_txt['file size']=file_length
            metadata_txt['file type']=file_name[1]
            
            output['File Data']= file_tmp
            output['File MetaData'] = metadata_txt
            return jsonify(output)

        elif file_name[1] == 'csv':
            metadata_csv={}
            fileString = file.read().decode('utf-8')
            datafile = [{k: v for k, v in row.items()} for row in csv.DictReader(fileString.splitlines(), skipinitialspace=True)]
            metadata_csv['file name']=file_name[0]
            metadata_csv['file type']=file_name[1]
            file.seek(0, os.SEEK_END)
            file_length = file.tell()
            metadata_csv['file size']= file_length
            output['File Data']= datafile
            output['File MetaData'] = metadata_csv
            return jsonify(output)

        elif file_name[1]=='png':
            metadata_png={}
            encoded_string = base64.b64encode(file.read())
            encoded_string = encoded_string.decode('utf-8')
            metadata_png['file name']=file_name[0]
            file.seek(0,os.SEEK_END)
            file_length = file.tell()
            metadata_png['file type']=file_name[1]
            metadata_png['file size']= file_length          
            #encoded_string = base64.b64encode(file.read().decode('utf-8'))
            output['File data']=encoded_string
            output['File Metadata']=metadata_png
            return jsonify(output)


    else:
        resp = jsonify({'message' : 'Allowed file types are txt, csv, png'})
        resp.status_code = 400
        return resp
示例#52
0
def upload():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        print(request.form['token'])
        # if user does not select file, browser also
        # submit a empty part without filename
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            ext = filename.split(".")[1]
            file.save(
                os.path.join(app.config['UPLOAD_FOLDER'],
                             "input_image" + "." + "jpg"))
            tempfile = os.path.join(app.config['UPLOAD_FOLDER'],
                                    "input_image" + "." + "jpg")
            # imagedataFile = open('/app/public/imagedataFile.txt','w')
            # imagedataFile.write(tempfile)
            # imagedataFile.close()
            img = open(tempfile, 'rb')  # reading file from glitch's local
            bitimage = base64.b64encode(img.read())
            img.close()

            req_data = {
                "requests": [{
                    "image": {
                        "content": bitimage
                    },
                    "features": [{
                        "type": "LABEL_DETECTION",
                        "maxResults": 10
                    }]
                }]
            }

            rt = requests.post("%s?key=%s" %
                               (GOOGLE_CLOUD_VISION_URL, GOOGLE_API_KEY),
                               json.dumps(req_data),
                               headers={'content-type': 'application/json'})
            # print(r.text)
            r = rt.json()

            for i in range(len(r["responses"][0]["labelAnnotations"])):
                keyword = r["responses"][0]["labelAnnotations"][i][
                    "description"]
                result = sp.search(keyword, limit=1, type='playlist')

                if (len(result["playlists"]["items"]) > 0):
                    print(result["playlists"]["items"][0]["name"].encode(
                        'utf-8').strip())

                if (i < 1):
                    resulttrack = result["playlists"]["items"][0][
                        "name"].encode('utf-8').strip()

                    requestBody = {
                        "context_uri":
                        result["playlists"]["items"][0]["uri"].encode(
                            'utf-8').strip()
                    }

                    print(requestBody)

                    token = request.form['token']

                    auth = {"Authorization": "Bearer {0}".format(token)}
                    auth["Content-Type"] = "application/json"
                    print(auth)

                    req = requests.put(
                        "https://api.spotify.com/v1/me/player/play",
                        data=json.dumps(requestBody),
                        headers=auth)
                    print(req.text)

    return render_template("show-snapmusic.html")
示例#53
0
 def wrapper(content_path, **kwargs):
     return f(secure_filename(content_path), **kwargs)
示例#54
0
def upload():
    file = request.files['imagem']
    savePath = os.path.join(UPLOAD_FOLDER, secure_filename(file.filename))
    file.save(savePath)
    return 'Upload feito com sucesso'
示例#55
0
 def wrapper(chapter, page, **kwargs):
     return f(secure_filename(chapter),
              secure_filename(page) if page is not None else page, **kwargs)
示例#56
0
文件: index.py 项目: brolisdom/cripto
def setPath(file):
    filename = secure_filename(file.filename)
    path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
    file.save(path)
    return path
示例#57
0
def tool_upload(token):
    """
  tool upload form takes GET and POST methods
  renders the form for GET
  form submission handling for POST
  """
    payload = verify_email_token(token)
    if not payload:
        flash('Link expired or invalid, try again')
        return redirect(url_for('index'))

    form = toolUpload()
    form.authoremail.data = payload['authoremail']
    form.papername.data = payload['papername']

    if form.validate_on_submit():
        paper = Paper(paper_name=form.papername.data,
                      author_name=form.authorname.data,
                      author_email=form.authoremail.data,
                      tool_name=form.toolname.data,
                      link_to_pdf=form.linktopdf.data,
                      link_to_archive=form.linktoarchive.data,
                      link_to_tool_webpage=form.linktotoolwebpage.data,
                      link_to_demo=form.linktodemo.data,
                      bibtex=form.bibtex.data,
                      description=form.description.data,
                      year=form.year.data,
                      conference=form.conference.data,
                      category=form.category.data,
                      view_count=0)

        # print(form.tags.data)

        for tag in form.tags.data.split(","):
            tag_obj = db.session.query(Tag).filter(
                Tag.tagname == tag.strip()).first()
            if tag_obj is None:
                tag_obj = Tag(tagname=tag.strip())
            paper.tags.append(tag_obj)

        db.session.add(paper)
        db.session.flush()

        filenames = []
        fileurls = []
        filetypes = form.file_types.data.split(',')

        for file in form.all_files.data:
            if not isinstance(file, str):
                filenames.append(secure_filename(file.filename))

                filename = '{}/{}'.format(paper.id,
                                          secure_filename(file.filename))
                file.filename = filename  # updating the name with paper.id for easy access

                s3_url = "{0}/{1}/{2}".format(app.config['S3_ENDPOINT'],
                                              app.config['S3_BUCKET'],
                                              filename)

                upload_file_to_s3(s3, file, app.config['S3_BUCKET'])
                fileurls.append(s3_url)

        # print("Uploaded files below for paper: {}".format(paper.id))
        # print(fileurls)

        for filename, fileurl, filetype in zip(filenames, fileurls, filetypes):
            paper.files.append(
                File(filename=filename, filetype=filetype, fileurl=fileurl))

        db.session.flush()
        db.session.commit()

        flash('Tool submission success!')

        return redirect(url_for('specific_paper', id=paper.id))

    # print(form.errors)

    return render_template('tool_upload.html',
                           title="Upload your tool here",
                           form=form)
示例#58
0
def upload():
    """
        This function basically parses a .csv or .xlsx or .txt or a file from a url and saves to the database.
    """
        
    # UPLOADED FILE
    uploaded_file = request.files["file"]

    filename = secure_filename(uploaded_file.filename)

    if not uploaded_file:
        return make_response({
            "message" : "Please upload a .csv or .xlsx file or enter the file's URL."
        }, 400
    )

    if filename != "":
        extension = os.path.splitext(filename)[-1].lower()
        if extension not in app.config["UPLOAD_EXTENSIONS"]:
            return make_response({
                "message" : f"Cannot process .{extension} files. Please upload a valid file with extension .csv, .xlsx, or enter the URL."
            }), 500
        
    uploaded_file.save(os.path.join(app.config["UPLOADS_DIR"], filename))

    if filename.endswith(".csv"):
        data = readCSV(os.path.join(app.config["UPLOADS_DIR"], filename))
    elif filename.endswith(".xlsx"):
        data = readXLSX(os.path.join(app.config["UPLOADS_DIR"], filename))



    for record in data:
        
        d = json.dumps({
            "phone_number" : record["phone_number"],
            "run_time" : record["run_time"],
            "created_at" : datetime.now()
        }, cls = DateTimeEncoder)
        
        d = data_schema_single.loads(d)
        
        d = Data(
            phone_number = d["phone_number"],
            run_time = d["run_time"],
            created_at = d["created_at"]
        )
        
        db.session.add(d)

        try:
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            pass

    # result = data_schema_multiple.dump(data)
        
    return make_response(
        { "message" : f"{len(data)} row(s) added successfully !."},
        201
    )
示例#59
0
def addVideo():
    file = request.files['video']
    timestamp = request.form.get("time")
    location = request.form.get("location")
    name = file.filename
    name = os.path.splitext(name)[0]
    if file and allowed_file(file.filename):
        if file.filename == None:
            filename = "Unknown_video"
        else:
            filename = secure_filename(file.filename)

    tmz_str = ' GMT+0530 (India Standard Time)'
    if timestamp.endswith(tmz_str):
        timestamp = timestamp.replace(tmz_str, '')

    date_time_obj = None
    try:
        date_time_obj = datetime.strptime(timestamp, '%a %b %d %Y %H:%M:%S')
    except Exception as e:
        pass
    if date_time_obj == None:
        return jsonify({
            "success": False,
            "message": "Timestamp is invalid. Please try again!"
        }), 403

    oid = fs.upload_from_stream(filename, file)
    video_name = 'saves/' + randomString() + '.mp4'
    f = open(video_name, 'wb+')
    fs.download_to_stream(oid, f)
    f.close()
    try:
        metadata = getFirstFrame(video_name)
        thumbnail = metadata[0]
        duration = time.strftime("%H:%M:%S", time.gmtime(metadata[1]))
    except Exception as e:
        print(e)
        if os.path.exists(video_name):
            os.remove(video_name)
        return jsonify({"success": False, "message": "Failed to process video"}), 500

    thumbnail_oid = fs.upload_from_stream(str(oid), thumbnail)
    # To check if image is saved
    # f_img = open('saves/frame2.jpg','wb+')
    # fs.download_to_stream(thumbnail_oid, f_img)
    # f_img.close()

    # insert video details
    db.video.insert_one({
        "name": name,
        "date": str(date_time_obj.date()),
        "time": str(date_time_obj.time()),
        "location_id": location,
        "file_id": str(oid),
        "thumbnail_id": str(thumbnail_oid),
        "duration": duration,
        "processing": False,
        "prepared": False
    })

    if os.path.exists(video_name):
        os.remove(video_name)

    return jsonify({
        "success": True,
        "message": "Video successfully uploaded"
    }), 200
示例#60
0
def delete_blog_picture(filename):
    delete_file = secure_filename(filename)
    os.remove(os.path.join(app.config['BLOG_UPLOAD_FOLDER'], delete_file))
    return