Example #1
0
def mediamgnt(filename=None):
    if request.method == "GET":
        if "fileid" not in request.args:
            abort(400)
        else:
            media = Media.get_by_id(request.args["fileid"])
            if not media or media.filename != filename:
                abort(404)
            return send_from_directory(
                current_app.config["UPLOAD_FOLDER"],
                media.local_filename
            )

    elif request.method == "POST":
        f = request.files["files[]"]
        if f:
            filename = f.filename
            version = Media.get_version(filename)
            local_filename = Media.new_local_filename(filename, version)
            filepath = os.path.join(
                current_app.config['UPLOAD_FOLDER'], local_filename)
            f.save(filepath)
            filesize = os.stat(filepath).st_size

            now = datetime.now()
            hashstr = local_filename + now.strftime("%Y-%m-%d %H:%M:%S")
            hashstr = hashstr.encode("utf8")
            media = Media(
                fileid=hashlib.sha256(hashstr).hexdigest(),
                filename=filename,
                version=Media.get_version(filename),
                content_type=f.content_type,
                size=filesize,
                create_time=now,
                display=True
            )

            media.save()
            return json.dumps(
                {"files": []})

    elif request.method == "DELETE":
        removelist = request.json
        for eachfile in removelist:
            fileid = eachfile["fileid"]
            filename = eachfile["filename"]
            onemedia = Media.get_by_id(fileid)
            if onemedia.filename != filename:
                continue
            onemedia.delete()
        return jsonify(
            success=True,
            message="success")
Example #2
0
def update_media(id):

	payload = request.form.to_dict()

	if 'www.youtube.com/watch?v=' in payload['url']:
		payload['media_type'] = 'video'
	else:
		return jsonify(data={}, status={'code': 401, 'message': 'URL input is not valid.'})
	
	if payload['media_type'] == 'video':


		v_location = payload['url'].index('v')

		eq_location = payload['url'].index('=')

		if eq_location == v_location + 1:
			url_id = payload['url'][eq_location + 1: eq_location + 12]
		else:
			return jsonify(data={}, status={'code': 401, 'message': 'URL input is not valid.'})


		payload['full_html'] = '<iframe width="560" height="315" src="https://www.youtube.com/embed/{}" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>'.format(url_id)

		payload['thumbnail_html'] = 'http://i.ytimg.com/vi/{}/maxresdefault.jpg'.format(url_id)


	query = Media.update(**payload).where(Media.id == id)
	query.execute()

	media_dict = model_to_dict(Media.get_by_id(id))

	return jsonify(data=media_dict, status={'code': 201, 'message': 'Success'}) 
Example #3
0
def reverse():
    reverselist = request.json
    for item in reverselist:
        media = Media.get_by_id(item["fileid"])
        if media:
            media.display = not media.display
            media.save()
    return jsonify(success=True,
                   message="success")
Example #4
0
def get_one_media(id):

	try:
		media = model_to_dict(Media.get_by_id(id))

		comments = Comment.select().where(Comment.media_id == media['id'])
		comments_dict = [model_to_dict(comment, exclude=[Comment.user_id.password]) for comment in comments]
		media['comments'] = comments_dict

		favorites = Favorite.select().where(Favorite.media_id == media['id'])
		favorites_dict = [model_to_dict(favorite, exclude=[Comment.user_id.password]) for favorite in favorites]
		media['favorites'] = favorites_dict

		return jsonify(data=media, status={"code": 200, "message": "Success"})

	except Media.DoesNotExist:
		return jsonify(data={}, status={"code": 401, "message": "There was an error getting the resource"})
Example #5
0
 def get_record(self, media_id):
     logger.info('NdbMediaDao:: DBHIT: get_record for %s ' % media_id)
     return Media.get_by_id(long(media_id))