from libs.api.apiBlueprint import ApiBlueprint from libs.app.common.paths import ROOT_DIR from flask import jsonify, request, send_from_directory import os import copy import random MEDIAS_FOLDER = os.path.join(ROOT_DIR, "files") class Blueprint(ApiBlueprint): def routes(self): from .p2private import P2private self.module:P2private = self.module @self.blueprint.route('/') def show(): return self.module.name @self.blueprint.route('/friends', methods=["GET", "POST", "DELETE"]) def friends(): if request.method == "GET": return jsonify(self.module.data.list_friends()) elif request.method == "POST": content:dict = request.json self.module.add_friend(content["pubkey"], content["relays"]) return jsonify() elif request.method == "DELETE": pass @self.blueprint.route('/messages//', methods=["GET", "POST"]) def messages(): if request.method == "GET": return jsonify() elif request.method == "POST": content:dict = request.json message = self.module.create_message(content["from"], content["to"], content["content"], content["medias"]) return jsonify(message) # @self.blueprint.route('/posts', methods=["GET", "POST"]) # def create_post(): # if request.method == "GET": # raw_posts = self.module.data_store.data["posts"] # posts = copy.deepcopy(raw_posts) # for post in posts: # for media in post.get("medias"): # if media.get("type") == "local": # media_info = self.module.data_store.get_media(media.get("hash")) # media["file_path"] = media_info.get("file_path") # return jsonify(posts) # elif request.method == "POST": # content:dict = request.json # hash = self.module.create_post(content["user"], content["content"], content["medias"], content["networks"]) # return jsonify(hash) # @self.blueprint.route('/medias', methods=['POST']) # def upload_file(): # file = request.files.get("file") # if not file: # return jsonify({"error": "Empty file"}), 400 # file_bytes = file.read() # file_hash = hash_bytes(file_bytes) # file_ext = os.path.splitext(file.filename)[1] # file_path = os.path.join(MEDIAS_FOLDER, f"{file_hash}{file_ext}") # if not os.path.exists(file_path): # with open(file_path, "wb") as f: # f.write(file_bytes) # return jsonify({ # "status": "ok", # "hash": file_hash, # "path": f"{file_hash}{file_ext}" # }) # @self.blueprint.route('/medias/') # def media_file(filename): # return send_from_directory(MEDIAS_FOLDER, filename)