Commit d3192b37 authored by Paarth's avatar Paarth

Merge branch 'master' of https://git.cse.iitb.ac.in/ayushjangir/sfcode

Merge
parents eabedff3 9c05f9d9
...@@ -17,11 +17,11 @@ if (isset($postData) && !empty($postData)) { ...@@ -17,11 +17,11 @@ if (isset($postData) && !empty($postData)) {
$request = json_decode($postData, true); $request = json_decode($postData, true);
$isFile = trim($request['isFile']); $isFile = trim($request['isFile']);
$nFiles = trim($request['nFiles']);
if($isFile){ if($isFile){
$username = mysqli_real_escape_string($mysqli, trim($request['file']['username'])); $username = mysqli_real_escape_string($mysqli, trim($request['file']['username']));
$filename = trim($request['file']['filename']); $filename = trim($request['file']['filename']);
$lang = trim($request['file']['language']); $lang = trim($request['file']['language']);
$nFiles = trim($request['nFiles']);
$path = '../users/' . $username . '/' . trim($request['file']['path']) . '/' . $filename . $lang; $path = '../users/' . $username . '/' . trim($request['file']['path']) . '/' . $filename . $lang;
$exec_path = '../user_execs/' . $username . '/' . trim($request['file']['path']) . '/' . $filename; $exec_path = '../user_execs/' . $username . '/' . trim($request['file']['path']) . '/' . $filename;
...@@ -36,7 +36,7 @@ if (isset($postData) && !empty($postData)) { ...@@ -36,7 +36,7 @@ if (isset($postData) && !empty($postData)) {
mysqli_query($mysqli,$sql); mysqli_query($mysqli,$sql);
$msg = "file successfully deleted"; $msg = "file successfully deleted";
echo json_encode($msg); echo json_encode($msg);
if (file_exists($exec_path)){ if (file_exists($exec_path) && is_file($exec_path)) {
unlink($exec_path); unlink($exec_path);
} }
} }
...@@ -48,7 +48,7 @@ if (isset($postData) && !empty($postData)) { ...@@ -48,7 +48,7 @@ if (isset($postData) && !empty($postData)) {
$username = trim($request['username']); $username = trim($request['username']);
$dirname = trim($request['file']['name']); $dirname = trim($request['file']['name']);
$path = '../users/' . $username . '/' . trim($request['file']['path']) . '/' . $dirname; $path = '../users/' . $username . '/' . trim($request['file']['path']) . '/' . $dirname;
$sql = "UPDATE users SET n_files=n_files-$nFiles WHERE username = '$username'"; $sql = "UPDATE users SET n_files=n_files-" . "$nFiles" . " WHERE username = '$username'";
mysqli_query($mysqli,$sql); mysqli_query($mysqli,$sql);
$exec_path = '../user_execs/' . $username . '/' . trim($request['file']['path']) . '/' . $dirname; $exec_path = '../user_execs/' . $username . '/' . trim($request['file']['path']) . '/' . $dirname;
......
...@@ -26,7 +26,7 @@ if (isset($postData) && !empty($postData)) { ...@@ -26,7 +26,7 @@ if (isset($postData) && !empty($postData)) {
fwrite($myfile, $data); fwrite($myfile, $data);
fclose($myfile); fclose($myfile);
$msg = "file successfully saved"; $msg = "0";
clearstatcache(); clearstatcache();
echo json_encode($msg); echo json_encode($msg);
exit; exit;
...@@ -35,7 +35,7 @@ if (isset($postData) && !empty($postData)) { ...@@ -35,7 +35,7 @@ if (isset($postData) && !empty($postData)) {
$sql1 = "SELECT n_files from users where username = '$username'"; $sql1 = "SELECT n_files from users where username = '$username'";
$sql2 = "UPDATE users SET n_files = n_files+1 WHERE username = '$username'"; $sql2 = "UPDATE users SET n_files = n_files+1 WHERE username = '$username'";
$result = mysqli_query($mysqli, $sql1)->fetch_all()[0]; $result = mysqli_query($mysqli, $sql1)->fetch_all()[0];
if ($result[0] >= 10) { if ($result[0] >= 10) {
$msg = "maximum limit(10) reached"; $msg = "maximum limit(10) reached";
echo json_encode($msg); echo json_encode($msg);
...@@ -46,21 +46,21 @@ if (isset($postData) && !empty($postData)) { ...@@ -46,21 +46,21 @@ if (isset($postData) && !empty($postData)) {
fwrite($myfile, $data); fwrite($myfile, $data);
fclose($myfile); fclose($myfile);
$msg = "file successfully uploaded"; $msg = "1";
clearstatcache(); clearstatcache();
echo json_encode($msg); echo json_encode($msg);
} else { } else {
http_response_code(404); http_response_code(404);
} }
} }
} }
else{ else{
$myfile = fopen($path, "w"); $myfile = fopen($path, "w");
fwrite($myfile, $data); fwrite($myfile, $data);
fclose($myfile); fclose($myfile);
$msg = "file successfully uploaded"; $msg = "1";
clearstatcache(); clearstatcache();
echo json_encode($msg); echo json_encode($msg);
} }
......
...@@ -113,7 +113,7 @@ export class ArenaComponent implements OnInit { ...@@ -113,7 +113,7 @@ export class ArenaComponent implements OnInit {
path: 'attempts/' path: 'attempts/'
}; };
console.log(file); console.log(file);
this.fileService.upload(file).subscribe(data1 => { this.fileService.upload(file, true).subscribe(data1 => {
console.log(data1); console.log(data1);
this.runCodeService.compileFile(file).subscribe(data2 => { this.runCodeService.compileFile(file).subscribe(data2 => {
console.log(data2); console.log(data2);
......
...@@ -26,8 +26,8 @@ ...@@ -26,8 +26,8 @@
<div *ngIf="isNew" class="file-buttons"> <div *ngIf="isNew" class="file-buttons">
<button (click)="cancelled.emit(true)" [class.disabled]="creating" [disabled]="creating">Cancel</button> <button (click)="cancelled.emit(true)" [class.disabled]="creating" [disabled]="creating">Cancel</button>
<button (click)="isFile ? onCreateFile() : onCreateDir()" <button (click)="isFile ? onCreateFile() : onCreateDir()"
[class.disabled]="newName==='' || creating || isErrorCreating" [class.disabled]="newName==='' || present(newName) || creating || isErrorCreating"
[disabled]="newName==='' || creating || isErrorCreating">{{isErrorCreating ? 'Error' : (creating ? 'Creating...' : 'Create')}}</button> [disabled]="newName==='' || present(newName) || creating || isErrorCreating">{{isErrorCreating ? 'Error' : (creating ? 'Creating...' : 'Create')}}</button>
</div> </div>
</div> </div>
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
<app-file-dir-card (cancelled)="creatingNewSub = 0" (createDir)="createDirExec($event)" <app-file-dir-card (cancelled)="creatingNewSub = 0" (createDir)="createDirExec($event)"
(createFile)="createFileExec($event)" (createFile)="createFileExec($event)"
*ngIf="creatingNewSub !== 0" [isFile]="creatingNewSub === 2" [isNew]="true" *ngIf="creatingNewSub !== 0" [isFile]="creatingNewSub === 2" [isNew]="true"
[parentDirs]="this.directory.dirs" [parentFiles]="this.directory.files"
[level]="level + 1" [parentPath]="directory.path + directory.name + '/'"></app-file-dir-card> [level]="level + 1" [parentPath]="directory.path + directory.name + '/'"></app-file-dir-card>
<app-file-dir-card (deleteDir)="deleteDirExec($event)" (deleteFile)="deleteFileExec($event)" <app-file-dir-card (deleteDir)="deleteDirExec($event)" (deleteFile)="deleteFileExec($event)"
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
button { button {
float: right; float: right;
border: none; border: none;
user-select: none;
} }
.title { .title {
......
...@@ -19,6 +19,8 @@ export class FileDirCardComponent implements OnInit { ...@@ -19,6 +19,8 @@ export class FileDirCardComponent implements OnInit {
@Input() isNew: boolean; @Input() isNew: boolean;
@Input() parentPath: string; @Input() parentPath: string;
@Input() noEdits = false; @Input() noEdits = false;
@Input() parentDirs: Directory[];
@Input() parentFiles: File[];
deleting: boolean; deleting: boolean;
isExpanded = false; isExpanded = false;
...@@ -29,6 +31,8 @@ export class FileDirCardComponent implements OnInit { ...@@ -29,6 +31,8 @@ export class FileDirCardComponent implements OnInit {
creatingNewSub = 0; creatingNewSub = 0;
extensions = ['.cpp', '.py', '.java']; extensions = ['.cpp', '.py', '.java'];
lang = 0; lang = 0;
filenames = [];
dirnames = [];
@Output() deleteFile: EventEmitter<any> = new EventEmitter(); @Output() deleteFile: EventEmitter<any> = new EventEmitter();
@Output() deleteDir: EventEmitter<any> = new EventEmitter(); @Output() deleteDir: EventEmitter<any> = new EventEmitter();
...@@ -40,12 +44,16 @@ export class FileDirCardComponent implements OnInit { ...@@ -40,12 +44,16 @@ export class FileDirCardComponent implements OnInit {
} }
ngOnInit(): void { ngOnInit(): void {
if (!this.isFile && !this.isNew) {
this.filenames = this.directory.files.map(item => item.filename);
this.dirnames = this.directory.dirs.map(item => item.name);
}
} }
onDelete(): void { onDelete(): void {
this.deleting = true; this.deleting = true;
this.fileService.delete(this.isFile ? this.file : this.directory, this.isFile, this.fileService.delete(this.isFile ? this.file : this.directory, this.isFile,
JSON.parse(this.apiService.getToken()).username).subscribe( JSON.parse(this.apiService.getToken()).username, this.isFile ? 0 : this.countFiles(this.directory)).subscribe(
data => { data => {
console.log(data); console.log(data);
if (this.isFile) { if (this.isFile) {
...@@ -112,13 +120,15 @@ export class FileDirCardComponent implements OnInit { ...@@ -112,13 +120,15 @@ export class FileDirCardComponent implements OnInit {
text: '', text: '',
path: this.parentPath path: this.parentPath
}; };
this.fileService.upload(file).subscribe(data => { this.fileService.upload(file, false).subscribe(data => {
console.log(data); if (data === '1') {
this.createFile.emit(file); console.log(data);
}, error => { this.createFile.emit(file);
console.log(error); } else {
this.creating = false; console.log(data);
this.isErrorCreating = true; this.creating = false;
this.isErrorCreating = true;
}
}); });
} }
...@@ -136,4 +146,20 @@ export class FileDirCardComponent implements OnInit { ...@@ -136,4 +146,20 @@ export class FileDirCardComponent implements OnInit {
}); });
this.creatingNewSub = 0; this.creatingNewSub = 0;
} }
private countFiles(directory: Directory): number {
let count = directory.files.length;
for (const dir of directory.dirs) {
count += this.countFiles(dir);
}
return count;
}
present(newName: string): boolean {
if (this.isFile) {
return this.parentFiles.map(item => item.filename + item.language).includes(newName + this.extensions[this.lang]);
} else {
return this.parentDirs.map(item => item.name).includes(newName);
}
}
} }
...@@ -38,8 +38,8 @@ export class FileService { ...@@ -38,8 +38,8 @@ export class FileService {
return of(ret); return of(ret);
} }
upload(file: File): Observable<any> { upload(file: File, isAttempt: boolean): Observable<any> {
return this.http.post(this.saveUrl, file); return this.http.post(this.saveUrl, {file, isAttempt});
} }
upload2(file): Observable<any> { upload2(file): Observable<any> {
...@@ -48,8 +48,8 @@ export class FileService { ...@@ -48,8 +48,8 @@ export class FileService {
return this.http.post(this.uploadUrl, formData); return this.http.post(this.uploadUrl, formData);
} }
delete(file: any, isFile: boolean, username: string): Observable<any> { delete(file: any, isFile: boolean, username: string, nFiles: number): Observable<any> {
return this.http.post(this.deleteUrl, {file, isFile, username}); return this.http.post(this.deleteUrl, {file, isFile, username, nFiles});
} }
getFileList(username: string): Observable<any> { getFileList(username: string): Observable<any> {
......
...@@ -4,10 +4,10 @@ ...@@ -4,10 +4,10 @@
</div> </div>
<div class="container"> <div class="container">
{{mainDir.dirs.length}} directories, {{mainDir.files.length}} files: {{mainDir.dirs.length}} directories, {{mainDir.files.length}} files (Total {{countFiles(mainDir)}} files):
</div> </div>
<app-file-dir-card *ngIf="creatingNew !== 0" [parentPath]="''" [isNew]="true" [isFile]="creatingNew === 2" (createFile)="createFileExec($event)" (createDir)="createDirExec($event)" (cancelled)="creatingNew = 0"></app-file-dir-card> <app-file-dir-card *ngIf="creatingNew !== 0" [parentPath]="''" [isNew]="true" [isFile]="creatingNew === 2" (createFile)="createFileExec($event)" (createDir)="createDirExec($event)" (cancelled)="creatingNew = 0" [parentDirs]="this.mainDir.dirs" [parentFiles]="this.mainDir.files"></app-file-dir-card>
<app-file-dir-card *ngFor="let dir of mainDir.dirs" [isFile]="false" [directory]="dir" [level]="0" [trace]="[mainDir.dirs.indexOf(dir)]" (deleteDir)="deleteDirExec($event)" (deleteFile)="deleteFileExec($event)" [isNew]="false" [noEdits]="dir.name === 'attempts'"></app-file-dir-card> <app-file-dir-card *ngFor="let dir of mainDir.dirs" [isFile]="false" [directory]="dir" [level]="0" [trace]="[mainDir.dirs.indexOf(dir)]" (deleteDir)="deleteDirExec($event)" (deleteFile)="deleteFileExec($event)" [isNew]="false" [noEdits]="dir.name === 'attempts'"></app-file-dir-card>
<app-file-dir-card *ngFor="let file of mainDir.files" [isFile]="true" [file]="file" [level]="0" [trace]="[mainDir.files.indexOf(file)]" (deleteDir)="deleteDirExec($event)" (deleteFile)="deleteFileExec($event)" [isNew]="false"></app-file-dir-card> <app-file-dir-card *ngFor="let file of mainDir.files" [isFile]="true" [file]="file" [level]="0" [trace]="[mainDir.files.indexOf(file)]" (deleteDir)="deleteDirExec($event)" (deleteFile)="deleteFileExec($event)" [isNew]="false"></app-file-dir-card>
......
...@@ -125,4 +125,12 @@ export class FileComponent implements OnInit { ...@@ -125,4 +125,12 @@ export class FileComponent implements OnInit {
}); });
this.creatingNew = 0; this.creatingNew = 0;
} }
public countFiles(directory: Directory): number {
let count = directory.files.length;
for (const dir of directory.dirs) {
count += this.countFiles(dir);
}
return count;
}
} }
...@@ -134,7 +134,7 @@ int main() { ...@@ -134,7 +134,7 @@ int main() {
} }
this.isUploading = true; this.isUploading = true;
btn.innerHTML = 'Saving...'; btn.innerHTML = 'Saving...';
this.fileService.upload(this.file) this.fileService.upload(this.file, false)
.subscribe(data => { .subscribe(data => {
btn.innerHTML = 'Saved'; btn.innerHTML = 'Saved';
this.isUploading = false; this.isUploading = false;
......
...@@ -31,7 +31,7 @@ export class SaveFileComponent implements OnInit { ...@@ -31,7 +31,7 @@ export class SaveFileComponent implements OnInit {
submitFile(): void { submitFile(): void {
this.isUploading = true; this.isUploading = true;
this.fileService.upload(this.file) this.fileService.upload(this.file, false)
.subscribe((response) => { .subscribe((response) => {
this.isUploading = false; this.isUploading = false;
this.savedFile.emit(true); this.savedFile.emit(true);
......
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