Skip to content

Commit

Permalink
music
Browse files Browse the repository at this point in the history
test
  • Loading branch information
deanpn2026 committed Jan 28, 2024
1 parent a71696a commit f0949f7
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 12 deletions.
Binary file modified backend/__pycache__/api.cpython-311.pyc
Binary file not shown.
Binary file modified backend/components/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
5 changes: 5 additions & 0 deletions backend/components/backend_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ class school(BaseModel):
name: str
email: str

<<<<<<< Updated upstream

=======
class song(BaseModel):
name: str
>>>>>>> Stashed changes
19 changes: 13 additions & 6 deletions backend/create_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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()
Binary file modified backend/endpoints/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file modified backend/endpoints/__pycache__/post.cpython-311.pyc
Binary file not shown.
11 changes: 8 additions & 3 deletions backend/endpoints/get.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
14 changes: 14 additions & 0 deletions backend/endpoints/post.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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}


6 changes: 4 additions & 2 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div>
Expand All @@ -15,6 +15,8 @@ const App = () => {
<Route path="/SignUp" element={<SignUpPage />} />
<Route path="/Profile" element={<App_profile />} />
<Route path="/Login" element={<LoginPage />} />
<Route path="/MusicUpload" element={<MusicUploader />} />
<Route path="/MusicPlay" element={<MusicPlayer />} />
</Routes>
</ChakraBaseProvider>
</div>
Expand Down
115 changes: 115 additions & 0 deletions frontend/src/components/MusicUpload.js
Original file line number Diff line number Diff line change
@@ -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 (
<div className="App">
<input
type="file"
onChange={ async(e) => {
uploadMusic(e);
await setUrl(base64ToBlob(baseMusic));
handlePlay();
}}
/>
<br></br>
<img src={baseMusic} height="200px" />
=======
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 (
<div className="App">
<form onSubmit={handleSubmit}>
<input
type="file"
onChange={handleFileInputChange}
/>
<button type="submit">Upload</button>
</form>
<br></br>
>>>>>>> Stashed changes
</div>

);
}

export default MusicUploader;
2 changes: 1 addition & 1 deletion frontend/src/utils/util.js
Original file line number Diff line number Diff line change
@@ -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,
});

Expand Down

0 comments on commit f0949f7

Please sign in to comment.