Commit 2ce9ffeb authored by Ayush's avatar Ayush

Debug file upload backend

parent 82a0168e
......@@ -3,43 +3,41 @@
include_once("database.php");
$postData = file_get_contents("php://input");
if(isset($postData) && !empty($postData)) {
$request = json_decode($postData);
$username = trim($request->File.id);
$data = trim($request->File.text);
$filename = trim($request->File.filename);
$lang = trim($request->File.language);
$path = trim($request->File.path).'/'.$filename;
if (isset($postData) && !empty($postData)) {
$request = json_decode($postData);
$username = trim($request->username);
$data = trim($request->text);
$filename = trim($request->filename);
$lang = trim($request->language);
$path = '../users/' . $username . '/' . trim($request->path) . '/' . $filename . $lang;
if(file_exists($path)) {
$msg = "file already exists";
clearstatcache();
echo json_encode($msg);
}
else {
$sql1 = "SELECT n_files from userdata where username = $username";
$sql2 = "UPDATE userdata SET n_files = n_files+1 WHERE username = $username ";
$result = mysqli_query($mysqli,$sql1);
if($result == 10){
$msg = "maximum limit(10) exceeded";
echo json_encode($msg);
}
else{
if($result = mysqli_query($mysqli, $sql2)) {
$myfile = fopen($path,"w");
fwrite($myfile,$data);
fclose($myfile);
if (file_exists($path)) {
$msg = "file already exists";
clearstatcache();
echo json_encode($msg);
} else {
// $sql1 = "SELECT n_files from userdata where username = $username";
// $sql2 = "UPDATE userdata SET n_files = n_files+1 WHERE username = $username ";
// $result = mysqli_query($mysqli, $sql1);
// if($result == 10){
// $msg = "maximum limit(10) exceeded";
// echo json_encode($msg);
// }
// else{
// if($result = mysqli_query($mysqli, $sql2)) {
$myfile = fopen($path, "w");
fwrite($myfile, $data);
fclose($myfile);
$msg = "file successfully uploaded";
clearstatcache();
echo json_encode($msg);
}
else{
http_response_code(404);
}
}
$msg = "file successfully uploaded";
clearstatcache();
echo json_encode($msg);
// }
// else{
// http_response_code(404);
// }
}
}
}
?>
......@@ -18,12 +18,12 @@ if (isset($postData) && !empty($postData)) {
'id' => mysqli_insert_id($mysqli),
'username' => $username
];
mkdir($authData['id']);
mkdir('../users/' . $authData['username']);
echo json_encode($authData);
}
}
else {
echo "mysqli_error($mysqli)";
}
......
......@@ -59,11 +59,11 @@ export class ArenaComponent implements OnInit {
const username = JSON.parse(this.apiService.getToken()).username;
for (const ext of extensions) {
this.files.push({
id: null,
username,
filename,
language: ext,
text: ''
text: '',
path: '/'
});
}
......
......@@ -18,7 +18,7 @@ export class AuthGuard implements CanActivate {
return this.isLogin(routeUrl);
}
isLogin(routeUrl: string) {
isLogin(routeUrl: string): boolean {
if (this.dataService.isLoggedIn()) {
return true;
}
......
......@@ -9,7 +9,7 @@ import {HttpClient} from '@angular/common/http';
})
export class FileService {
baseApiUrl = 'https://file.io';
baseApiUrl = 'http://localhost/sfcode/backend/filesave.php';
constructor(private http: HttpClient) {
}
......@@ -23,10 +23,10 @@ export class FileService {
for (const item of arr) {
ret.push({
username: '',
id: item,
filename: 'lambda',
language: '.cpp',
text: 'Random Shit'
text: 'Random Shit',
path: '/'
});
}
......@@ -34,9 +34,7 @@ export class FileService {
}
upload(file): Observable<any> {
const formData = new FormData();
formData.append('file', new Blob([new TextEncoder().encode(file)], {type: 'application/json'}), file.name);
return this.http.post(this.baseApiUrl, formData);
return this.http.post(this.baseApiUrl, file);
}
}
export interface File {
id: number;
username: string;
filename: string;
language: string;
text: string;
path: string;
}
......@@ -58,7 +58,7 @@
</div>
<p *ngIf="!loading">
<button (click)="uploadPopupActive = false;" id="upload-cancel-btn">Close</button>
<button (click)="onUpload()" id="file-upload-btn">Upload</button>
<button (click)="onUpload()" id="file-upload-btn" [class.disabled]="fileToUpload == null || fileToUpload == undefined">Upload</button>
</p>
<p *ngIf="loading" style="margin-top: 20px; text-align: center">
Uploading...
......
......@@ -30,9 +30,13 @@ export class FileComponent implements OnInit {
onChange(event): void {
this.fileToUpload = event.target.files[0];
console.log(this.fileToUpload);
}
onUpload(): void {
if ((document.getElementById('file-upload-input') as HTMLInputElement).value === '') {return; }
this.loading = true;
console.log(this.fileToUpload);
......
......@@ -23,11 +23,11 @@ export class IdeComponent implements OnInit {
isUploading = false;
isUpToDate = false;
file: File = {
id: null,
filename: 'Untitled',
language: '.cpp',
text: '',
username: JSON.parse(this.apiService.getToken()).username,
path: '/'
};
@ViewChild(InputComponent) inputField;
@ViewChild(IdeCompileComponent) runField;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment