Commit 2ce9ffeb authored by Ayush's avatar Ayush

Debug file upload backend

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