You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
208 lines
6.8 KiB
TypeScript
208 lines
6.8 KiB
TypeScript
import { Component, OnInit, OnDestroy } from '@angular/core';
|
|
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
|
import { Router, ActivatedRoute } from '@angular/router';
|
|
import { Observable } from 'rxjs/Observable';
|
|
import { tap, catchError } from 'rxjs/operators';
|
|
import { of } from 'rxjs/observable/of';
|
|
import {Http} from "@angular/http";
|
|
import {Form} from "@angular/forms";
|
|
import * as formData from 'form-data';
|
|
import {forEach} from '@angular/router/src/utils/collection';
|
|
|
|
@Component({
|
|
selector: 'app-book',
|
|
templateUrl: './book.component.html',
|
|
styleUrls: ['./book.component.css']
|
|
})
|
|
export class BookComponent implements OnInit, OnDestroy {
|
|
currentUser: any;
|
|
allUserFile: any;
|
|
allUserFolder: any;
|
|
mainFolder: any;
|
|
books: any;
|
|
filesToUpload: Array<File>;
|
|
fileChooseName: string;
|
|
httpOptions: any;
|
|
isClickCreateFolder: boolean;
|
|
inputCreateFolder: String;
|
|
|
|
constructor(private http: HttpClient, private router: Router, private route: ActivatedRoute) {
|
|
this.isClickCreateFolder = false;
|
|
this.fileChooseName = 'None';
|
|
this.filesToUpload = [];
|
|
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.httpOptions = {
|
|
headers: new HttpHeaders({ 'Authorization': localStorage.getItem('jwtToken') }),
|
|
pathFolder: ''
|
|
};
|
|
|
|
this.http.get('/api/getCurrentUser', this.httpOptions).subscribe(user => {
|
|
this.currentUser = user;
|
|
this.openFolder('Home');
|
|
}, err => {
|
|
if (err.status === 401) {
|
|
this.router.navigate(['login']);
|
|
}
|
|
});
|
|
}
|
|
|
|
deleteFile(fileToDelete) {
|
|
this.http.post('/api/deleteFileMongo', fileToDelete).subscribe(file => {
|
|
this.openFolder(this.mainFolder.path);
|
|
});
|
|
}
|
|
|
|
deleteFolder(folderToDelete) {
|
|
this.http.post('/api/deleteFolderMongo', folderToDelete).subscribe(folder => {
|
|
this.openFolder(this.mainFolder.path);
|
|
});
|
|
}
|
|
|
|
|
|
renameFolder(folderToRename){
|
|
folderToRename.path = this.mainFolder.path + '/' + folderToRename.name;
|
|
|
|
this.http.post('/api/renameFolderMongo', folderToRename).subscribe(data => {
|
|
console.log('data : ' + data);
|
|
});
|
|
}
|
|
|
|
renameFile(fileToRename){
|
|
this.http.post('/api/renameFileMongo', fileToRename).subscribe(data => {
|
|
console.log('data : ' + data);
|
|
});
|
|
}
|
|
|
|
getfiles() {
|
|
//var preview = document.querySelector('img');
|
|
|
|
|
|
|
|
this.http.post('/api/getFileList', {mainPath : this.mainFolder.path, owner: this.currentUser.username.toString()}).subscribe(files => {
|
|
if (files) {
|
|
this.allUserFile = files;
|
|
/*
|
|
for (let f of this.allUserFile) {
|
|
console.log('f.extention : ' + f.extention);
|
|
if (f.extention === ('text/plain')) {
|
|
this.reader = new FileReader();
|
|
let contenido;
|
|
console.log('ffff : ' + JSON.stringify(f));
|
|
this.reader.onload = (e) => {
|
|
contenido = this.reader.result;
|
|
console.log('contenido : ' + contenido);
|
|
};
|
|
this.reader.readAsText(f);
|
|
}
|
|
} */
|
|
} else {
|
|
this.allUserFile = [];
|
|
}
|
|
});
|
|
}
|
|
|
|
getfolders() {
|
|
this.http.post('/api/getFolderList', {mainPath : this.mainFolder.path, owner: this.currentUser.username.toString()}).subscribe(folders => {
|
|
if (folders) {
|
|
this.allUserFolder = folders;
|
|
} else {
|
|
this.allUserFolder = [];
|
|
}
|
|
});
|
|
}
|
|
|
|
backToParentFolder(){
|
|
this.openFolder(this.mainFolder.parent);
|
|
}
|
|
|
|
|
|
openFolder(path: String) {
|
|
this.httpOptions.pathFolder = path;
|
|
|
|
this.isClickCreateFolder = false;
|
|
|
|
this.http.post('/api/getMainFolder', {path: path, owner: this.currentUser.username.toString()}).subscribe(folder => {
|
|
if (folder) {
|
|
this.mainFolder = folder;
|
|
this.getfolders();
|
|
this.getfiles();
|
|
} else {
|
|
if (path === 'Home'){
|
|
this.http.post('api/createFolder', { name: 'Home', parent: '', path: 'Home', taille: 'taiile', idUser: this.currentUser._id.toString(), fileList: [], folderList: [], owner: this.currentUser.username.toString() }).subscribe(resp => {
|
|
this.mainFolder = resp;
|
|
}, err => {
|
|
console.log('errrrr folllder : ' + err);
|
|
});
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
createBool() {
|
|
this.isClickCreateFolder = true;
|
|
}
|
|
|
|
createFolder() {
|
|
var newPath = this.mainFolder.path + '/' + this.inputCreateFolder;
|
|
this.isClickCreateFolder = false;
|
|
|
|
this.http.post('api/createFolder', { name: this.inputCreateFolder.toString(), parent: this.mainFolder.path, path: newPath.toString(), taille: 'taiile', idUser: this.currentUser._id.toString(), fileList: [], folderList: [], owner: this.currentUser.username.toString() }).subscribe(resp => {
|
|
this.getfolders();
|
|
}, err => {
|
|
console.log('errrrr folllder : ' + err);
|
|
});
|
|
}
|
|
|
|
ngOnDestroy() {
|
|
localStorage.removeItem('jwtToken');
|
|
this.httpOptions = null;
|
|
this.currentUser = null;
|
|
}
|
|
|
|
upload() {
|
|
if(this.mainFolder !== undefined){
|
|
this.http.post('api/uploadFileMongo', { name: this.fileChooseName.toString(), path: this.mainFolder.path.toString(), extention: this.filesToUpload[0].type.toString(), taille: this.filesToUpload[0].size.toString(), idUser: this.currentUser._id.toString(), owner: this.currentUser.username.toString(), lastDate: Date.now().toString() }).subscribe(resp => {
|
|
this.makeFileRequest('http://localhost:3000/api/upload', [], this.filesToUpload, resp).then((result) => {
|
|
this.getfiles();
|
|
this.filesToUpload = null;
|
|
this.fileChooseName = 'None';
|
|
}, (error) => {
|
|
console.error(error);
|
|
});
|
|
}, err => {
|
|
console.error(err);
|
|
});
|
|
}
|
|
}
|
|
|
|
fileChangeEvent(fileInput: any){
|
|
this.filesToUpload = <Array<File>> fileInput.target.files;
|
|
this.fileChooseName = this.filesToUpload[0].name;
|
|
}
|
|
|
|
makeFileRequest(url: string, params: Array<string>, files: Array<File>, monFile: any) {
|
|
return new Promise((resolve, reject) => {
|
|
var formData: any = new FormData();
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
formData.append("public[]", files[0], monFile._id);
|
|
xhr.onreadystatechange = function () {
|
|
if (xhr.readyState == 4) {
|
|
if (xhr.status == 200) {
|
|
resolve(JSON.parse(xhr.response));
|
|
} else {
|
|
reject(xhr.response);
|
|
}
|
|
}
|
|
};
|
|
|
|
xhr.open("POST", url, true);
|
|
xhr.send(formData);
|
|
});
|
|
}
|
|
|
|
}
|