From f0949f72ff0b19672e8fd3af37e64061717cfb29 Mon Sep 17 00:00:00 2001 From: Dean Nguyen Date: Sun, 28 Jan 2024 11:09:59 -0800 Subject: [PATCH] music test --- backend/__pycache__/api.cpython-311.pyc | Bin 625 -> 640 bytes .../__pycache__/__init__.cpython-311.pyc | Bin 215 -> 230 bytes backend/components/backend_components.py | 5 + backend/create_db.py | 19 ++- .../__pycache__/__init__.cpython-311.pyc | Bin 255 -> 270 bytes .../__pycache__/post.cpython-311.pyc | Bin 3763 -> 4375 bytes backend/endpoints/get.py | 11 +- backend/endpoints/post.py | 14 +++ frontend/src/App.js | 6 +- frontend/src/components/MusicUpload.js | 115 ++++++++++++++++++ frontend/src/utils/util.js | 2 +- 11 files changed, 160 insertions(+), 12 deletions(-) create mode 100644 frontend/src/components/MusicUpload.js diff --git a/backend/__pycache__/api.cpython-311.pyc b/backend/__pycache__/api.cpython-311.pyc index 9fe79c51e6ca1c1c512de0cbd8d6162448df6325..52db132ee659e26f30003362f80c38cf23c61ad3 100644 GIT binary patch delta 84 zcmey!(!k2QoR^o20SNX=Y)kdu$lJkaY3FPe6Iz^FR2-9%S74MK^$}9vuJx delta 69 zcmZo*{m8<*oR^o20SMOAZB2F9$lJlFrLP}aoLW?@pIw=gno(Mrm!a>Gn4FzhlAjk) Wl%JKFT%w-@7TvIWI340}#Y)*pjL|kvGi6&)F&_v^ce>I3^{pz$iP$CBHl`CqFTzIL1A* uB%?Gb#v?H~JFz4`FQ6ztD>b>>>>>> Stashed changes diff --git a/backend/create_db.py b/backend/create_db.py index 681f7a3..79ccbbf 100644 --- a/backend/create_db.py +++ b/backend/create_db.py @@ -36,6 +36,12 @@ def create_schools_table(): "CREATE TABLE schools(id INTEGER PRIMARY KEY UNIQUE NOT NULL, name TEXT UNIQUE NOT NULL, email_domain TEXT NOT NULL)" ) con.commit() +def create_music_file_table(): + with sqlite3.connect("db.db") as con: + con.execute( + "CREATE TABLE music_file(file_name STRING PRIMARY KEY, file BLOB)" + ) + con.commit() def insert_dummy_data(): @@ -72,9 +78,10 @@ def insert_dummy_data(): if __name__ == '__main__': - create_schools_table() - create_users_table() - create_pages_table() - create_communities_table() - create_tags_table() - insert_dummy_data() + # create_schools_table() + # create_users_table() + # create_pages_table() + # create_communities_table() + # create_tags_table() + # insert_dummy_data() + create_music_file_table() \ No newline at end of file diff --git a/backend/endpoints/__pycache__/__init__.cpython-311.pyc b/backend/endpoints/__pycache__/__init__.cpython-311.pyc index e85993cf5aec97c85273df3b405c422a786430cd..4767d37e27fda4d7c660f821a2b1d252d2348651 100644 GIT binary patch delta 92 zcmey**vG`XoR^o20SIC?Y)Q45$eZr$>uePhTAW%`9Fvk)V3Zx>l3$*elb@JU9OIr@ rl2MuzEp$5P;vay}RDuX0yruWRq=vDm89lBN0eZ1Vu_iTT}@up%!Vi(#jjZ1aG|S z)w5R!2-+YD%C&lja6tqPK`4St5C=|hL+XJIq-tadDMA9Zw{YzRW*qe=UUBBx9zV~_ zH#5)k?A@`=$<$ZLWQ@Qle)&DSqux&CrId7gy54OLRfZ@rh!L`eo7qaXnXBZQ`AXi` zNmij!kO-rV5oB=~;cZID4qV5>h%jYYGSpkjHYj!;sY#_6;GzL8#=^jL<9*<0089kH zB#Qu;>f^=&+^GOJz*OMUC%G;ml~Mp441gIH1#svj2;AuaHyq%yECgJxj~joAn}ru< zssHZ1Kl3qv^7E2jH) zr(V6PFRkjX-MY^023vBgb$EtcRk+%=*>as~+jiV?`&}=FlbU6(m@PgA!Bx!iA|c|w z{N3y+c;H14UbL;Ruv){hTTsbq^zkdTr^GMH3@wR&6nAz4Y?{o|=pxrm z+m8hJnEF7PhCbIie&fZVl)Y$y%QZsDCRrm}Fz+8Hzu1r9FYvy6lWfYLC?8Ofkn`O& zx(%i8VEO)lp8_=`lfo{PU-wkZh+B!{B4Ph2P^Q80H^W~=jl?g(%~P7x{HC;dXXDgk>Mel+pu|>ES6p zPh9D)Nk?G+-{gf8=?_S`_uuCMNv`|HA&u^?CgV;w;>Ja#6jy;RbfND31hZFg*L7E(GeJp{58#FSXYM)w-#Pa&Z?c?m_B*5s& z7xZ;ts^-NDGA%VHL=Xi^x@pYeF#M-P5YA!Fhapqn3(*L?@|{B9{BKw|$}Dlp5*MU0 zaG@10dySh2MtBK~&;WqZ6)<-V6byBVi%|u*_zIU_;u4G85u7+lg8-&hz`_!kUIc;5 zP(N_l6|QKAPlR%g*Te%7+8Y^U0=UrJ8TD|B`hVe+x$$~B6OL<0$vwOjBB$t8wZxKxqhCjL2n}vBg zykCgoIt$|Z=|J!FSqyy=tMX>Jl=t=(_fFDcRCj0tABffYhw%4g5uIzUqz^kx!ZJJj zOVoJi`q0k`7}-GCMya6eq0~Xn1ctPCQ&-B}fn|*Pw9Xzt3}4*7!B5n~$xHDP*8fdq W@lp}Q(Cl5TMpPk0QkN*+#{U4xgWc%> diff --git a/backend/endpoints/get.py b/backend/endpoints/get.py index 106c1ef..04142d7 100644 --- a/backend/endpoints/get.py +++ b/backend/endpoints/get.py @@ -1,5 +1,5 @@ from components import school -from database import get_all_schools, get_all_pages, get_school_from_user, get_data_from_user, get_pages_with_tags, load_page_with_user +from database import get_music, get_all_schools, get_all_pages, get_school_from_user, get_data_from_user, get_pages_with_tags, load_page_with_user from api import app import json import requests @@ -108,5 +108,10 @@ def get_user_page(username:str, school_id:int): return {json_response} - - +@app.get("/get_music/") +def get_user_page(song: str): + # get file + print(song) + result = get_music(song) + json_response = json.dumps({"file": result}) + return json_response diff --git a/backend/endpoints/post.py b/backend/endpoints/post.py index 33e3157..8130e1c 100644 --- a/backend/endpoints/post.py +++ b/backend/endpoints/post.py @@ -1,9 +1,15 @@ from api import app import json from components import userLogin, newUser, pageData, newBio, newTags, tagsSearch +<<<<<<< Updated upstream from database import get_user, get_email_domain, get_password_from_user, add_new_users, add_page_with_user, set_new_page_with_user, update_bio, update_tags, get_names_with_tags, get_all_users +======= +from database import add_music, get_user, get_email_domain, get_password_from_user, add_new_users, add_page_with_user, set_new_page_with_user, update_bio, update_tags, get_names_with_tags +from fastapi import Depends, UploadFile, File +import base64 +>>>>>>> Stashed changes @app.post("/login/") async def post_login(user_login: userLogin): @@ -58,3 +64,11 @@ async def post_create_user(user_info: newUser): # if not send back 1 # all good # send back a 2 + +@app.post("/add_music/") +async def post_add_music(file_upload: UploadFile): + data = await file_upload.read() + add_music(file_upload.filename, data) + return {"filename": 1} + + \ No newline at end of file diff --git a/frontend/src/App.js b/frontend/src/App.js index ab09ef9..2a36014 100644 --- a/frontend/src/App.js +++ b/frontend/src/App.js @@ -4,8 +4,8 @@ import App_profile from './pages/App_profile'; import { ChakraBaseProvider } from '@chakra-ui/react'; import {useState} from "react"; import LoginPage from './pages/Login'; -import "./index.css" - +import MusicUploader from './components/MusicUpload'; +import MusicPlayer from './components/MusicPlayer'; const App = () => { return (
@@ -15,6 +15,8 @@ const App = () => { } /> } /> } /> + } /> + } />
diff --git a/frontend/src/components/MusicUpload.js b/frontend/src/components/MusicUpload.js new file mode 100644 index 0000000..e7ef053 --- /dev/null +++ b/frontend/src/components/MusicUpload.js @@ -0,0 +1,115 @@ +import React, { useState } from "react"; +<<<<<<< Updated upstream + +function MusicUploader() { + const [baseMusic, setBaseMusic] = useState(""); + const [url, setUrl] = useState(); +======= +import { postAPI, getAPI} from "../utils/util"; +function MusicUploader() { +>>>>>>> Stashed changes + + const uploadMusic = async (e) => { + const file = e.target.files[0]; + const base64 = await convertBase64(file); +<<<<<<< Updated upstream + setBaseMusic(base64); + console.log(base64); +======= + console.log(base64) + await postAPI("/add_music/", {"music": base64, "postID": 1}); +>>>>>>> Stashed changes + }; + + const convertBase64 = (file) => { + return new Promise((resolve, reject) => { + const fileReader = new FileReader(); +<<<<<<< Updated upstream + fileReader.readAsDataURL(file); +======= + fileReader.readAsText(file); +>>>>>>> Stashed changes + + fileReader.onload = () => { + resolve(fileReader.result); + }; + + fileReader.onerror = (error) => { + reject(error); + }; + }); + }; + +<<<<<<< Updated upstream + const handlePlay = () => { + console.log("hi"); + console.log(url.text()); + const tmp = new Audio(url); //passing your state (hook) + tmp.volume = 1; + tmp.play() //simple play of an audio element. + } + + function base64ToBlob(base64String) { + const byteCharacters = atob(base64String); + const byteArrays = []; + + for (let i = 0; i < byteCharacters.length; i++) { + byteArrays.push(byteCharacters.charCodeAt(i)); + } + + const byteArray = new Uint8Array(byteArrays); + return new Blob([byteArray], { 'type': 'audio/mp3' }); +}; + + + return ( +
+ { + uploadMusic(e); + await setUrl(base64ToBlob(baseMusic)); + handlePlay(); + }} + /> +

+ +======= + const [file, setFile] = useState(null); + const handleFileInputChange = (event) => { + setFile(event.target.files[0]); + } + const handleSubmit = async(event) => { + event.preventDefault(); + const formData = new FormData(); + formData.append('file_upload', file); + + try { + const endpoint = "http://localhost:8000/add_music/" + const response = await fetch(endpoint, { + method: "POST", + body: formData + }); + console.log("send"); + } catch(error) { + console.log(error); + } + + } + return ( +
+
+ + +
+

+>>>>>>> Stashed changes +
+ + ); +} + +export default MusicUploader; \ No newline at end of file diff --git a/frontend/src/utils/util.js b/frontend/src/utils/util.js index c11afec..5e4a714 100644 --- a/frontend/src/utils/util.js +++ b/frontend/src/utils/util.js @@ -1,7 +1,7 @@ import axios from 'axios'; const Backend = axios.create({ - baseURL: 'http://169.234.115.114:8000/', + baseURL: 'http://localhost:8000/', withCredentials: false, });