Grosse mise a jour creation complete du file system dossier et PATH

This commit is contained in:
Tmadkaud 2018-05-13 18:17:18 +02:00
parent 650b013c55
commit 5a91dda627
4 changed files with 229 additions and 112 deletions

51
models/FolderMongo.js Normal file
View File

@ -0,0 +1,51 @@
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var FolderMongoShema = new Schema({
uid: {
type: String,
unique: false
},
name: {
type: String,
unique: false
},
path: {
type: String,
unique: false
},
taille: {
type: String,
unique: false
},
parent: {
type: String,
unique: false
},
fileList: {
type: [],
unique: false
},
folderList: {
type: [],
unique: false
},
idUser: {
type: String,
unique: false
},
owner: {
type: String,
unique: false
},
lastDate: {
type: String,
unique: false
},
createDate: {
type: String,
unique: false
}
});
module.exports = mongoose.model('FolderMongo', FolderMongoShema);

View File

@ -7,6 +7,7 @@ var jwt = require('jsonwebtoken');
var router = express.Router();
var User = require("../models/user");
var FileMongo = require("../models/FileMongo");
var FolderMongo = require("../models/FolderMongo");
var Book = require("../models/book");
var multer = require('multer');
@ -38,16 +39,25 @@ router.post('/signup', function(req, res) {
/* SAVE FILE */
router.post('/uploadFileMongo', function(req, res, next) {
console.log('req.body: :: ' + req.body.uid);
//console.log('req.body: :: ' + req.body.uid);
FileMongo.create(req.body, function (err, post) {
if (err){
console.log('err: :: ' + err);
return next(err);
}
res.json(post);
});
});
/* SAVE FOLDER */
router.post('/createFolder', function(req, res, next) {
FolderMongo.create(req.body, function (err, post) {
if (err){
return next(err);
}
res.json(post);
});
});
/* DELETE FILE */
router.post('/deleteFileMongo', function(req, res, next) {
@ -59,6 +69,53 @@ router.post('/deleteFileMongo', function(req, res, next) {
});
});
router.post('/getMainFolder', function(req, res) {
FolderMongo.findOne({
path: req.body.path,
owner: req.body.owner
}, function(err, folder) {
res.json(folder);
});
});
router.post('/getFolderList', function(req, res) {
FolderMongo.find({
parent: req.body.mainPath,
owner: req.body.owner
}, function(err, folder) {
res.json(folder);
});
});
router.post('/getFileList', function(req, res) {
FileMongo.find({
path: req.body.mainPath,
owner: req.body.owner
}, function(err, files) {
res.json(files);
});
});
router.get('/allUserFile', passport.authenticate('jwt', { session: false}), function(req, res) {
var token = getToken(req.headers);
if (token) {
FileMongo.find({
idUser: req.user._id,
owner: req.user.username
}, function(err, file) {
res.json(file);
});
} else {
return res.status(403).send({success: false, msg: 'Unauthorized.'});
}
});
router.post('/signin', function(req, res) {
User.findOne({
username: req.body.username
@ -106,16 +163,7 @@ router.post('/book', passport.authenticate('jwt', { session: false}), function(r
return res.status(403).send({success: false, msg: 'Unauthorized.'});
}
});
/*
router.post('/book/file', function(req, res) {
if(req.files){
//var file = req.files.filename;
//var filename = req.files.filename.name;
console.log(req.files);
}
});
*/
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './public/')
@ -127,52 +175,15 @@ const storage = multer.diskStorage({
//post qui va stocker la photo du user ajoute dans le register , multer permet de definir le dossier serveur dans lequel seront stockes les photos
router.post('/upload' , multer({storage: storage, limits: {fileSize: 30000000000}}).array("public[]", 12) ,function(req,res,next){
console.log("req.files[0].fieldname: "+req.files[0].fieldname);//ok
console.log("req.files[0].originalname: "+req.files[0].originalname);//ok
console.log("req.files.filename: "+req.files[0].filename);//ok
console.log("req.files.path: "+req.files[0].path);//ok
console.log("req.files.size: "+req.files[0].size);//ok
res.send(req.files);
});
router.get('/allUserFile', passport.authenticate('jwt', { session: false}), function(req, res) {
router.get('/getCurrentUser', passport.authenticate('jwt', { session: false}), function(req, res) {
var token = getToken(req.headers);
if (token) {
console.log('oooooo : ' + req.user._id + ' ::: kaka : ' + req.user.username);
FileMongo.find({
idUser: req.user._id,
owner: req.user.username
}, function(err, file) {
console.log('userrrr baa : ' + JSON.stringify(file));
res.json(file);
});
} else {
return res.status(403).send({success: false, msg: 'Unauthorized.'});
}
});
router.get('/book', passport.authenticate('jwt', { session: false}), function(req, res) {
var token = getToken(req.headers);
if (token) {
console.log('oooooo : ' + req.user._id + ' ::: kaka : ' + req.user.username);
FileMongo.find({
idUser: req.user._id,
owner: req.user.username
}, function(err, file) {
console.log('userrrr baa : ' + JSON.stringify(file));
});
console.log('req.user.username yiiiiii ::: ' + req.user.username);
res.json(req.user);
/*
Book.find(function (err, books) {
if (err) return next(err);
res.json(books);
});*/
} else {
return res.status(403).send({success: false, msg: 'Unauthorized.'});
}

View File

@ -1,11 +1,29 @@
<div class="container-fluid">
<div *ngIf="mainFolder">
<p> Path : {{mainFolder.path}}</p>
<p> Vous êtes dans le dossier : {{mainFolder.name}}</p>
<button type="button" class="btn btn-warning" (click)="backToParentFolder()" *ngIf="mainFolder.path !== 'Home'">Retour à : {{mainFolder.parent}}</button>
</div>
<button type="button" class="btn btn-warning" (click)="createBool()" *ngIf="isClickCreateFolder === false">Nouveau dossier</button>
<div class="md-form input-group" *ngIf="isClickCreateFolder === true">
<div class="input-group-btn">
<button class="btn btn-primary btn-lg waves-light" type="button" mdbWavesEffect (click)="createFolder()">Créer</button>
</div>
<input type="search" class="form-control" placeholder="Nom du nouveau dossier..." [(ngModel)]="inputCreateFolder">
</div>
<label class="btn btn-danger col-sm-2 col-md-2" >
Fichier à ajouter<input type="file" style="display: none;" #myPicture name="myPicture" (change)="fileChangeEvent($event)">
</label>
<button type="button" class="btn btn-warning" (click)="upload()">Ajouter le fichier {{fileChooseName}}</button>
<label> Vous avez choisi : {{fileChooseName}}</label>
</div>
<table class="table table-striped">
@ -19,6 +37,14 @@
</tr>
</thead>
<tbody>
<tr *ngFor="let folder of allUserFolder; let i = index;">
<td><button type="button" class="btn btn-warning" (click)="openFolder(folder.path)">Ouvrir dossier</button></td>
<td>{{ folder.name }}</td>
<td>{{ folder.taille}} Mo</td>
<td>Folder</td>
<td>{{ folder.owner }}</td>
<i class="fa fa-folder-open-o" aria-hidden="true"></i>
</tr>
<tr *ngFor="let file of allUserFile; let i = index;">
<th scope="row">{{ i + 1 }}</th>
<td>{{ file.name }}</td>
@ -26,10 +52,10 @@
<td>{{ file.extention }}</td>
<td>{{ file.lastDate }}</td>
<div *ngIf="file.extention === ('image/jpeg')">
<img src="../../../{{ file.path }}" class="img-fluid flex-center" width="150" height="150">
<img src="../../../public/{{ file._id.toString() }}" class="img-fluid flex-center" width="150" height="150">
</div>
<div *ngIf="file.extention === ('image/png')">
<img src="../../../{{ file.path }}" class="img-fluid flex-center" width="150" height="150">
<img src="../../../public/{{ file._id.toString() }}" class="img-fluid flex-center" width="150" height="150">
</div>
<div *ngIf="file.extention === ('application/pdf')">
<i class="fa fa-file-pdf-o" aria-hidden="true"></i>

View File

@ -16,88 +16,123 @@ import * as formData from 'form-data';
export class BookComponent implements OnInit, OnDestroy {
currentUser: any;
allUserFile: any;
allUserFolder: any;
mainFolder: any;
books: any;
filesToUpload: Array<File>;
fileChooseName: string;
httpOptions: any;
// tokenFromLogin: any;
constructor(private http: HttpClient, private router: Router, private route: ActivatedRoute) {
isClickCreateFolder: boolean;
inputCreateFolder: String;
constructor(private http: HttpClient, private router: Router, private route: ActivatedRoute) {
this.isClickCreateFolder = false;
this.fileChooseName = 'None';
this.filesToUpload = [];
}
ngOnInit() {
this.route.params.subscribe(params => {
// this.tokenFromLogin = params['tokenFromLogin'];
this.httpOptions = {
headers: new HttpHeaders({ 'Authorization': localStorage.getItem('jwtToken') })
headers: new HttpHeaders({ 'Authorization': localStorage.getItem('jwtToken') }),
pathFolder: ''
};
this.http.get('/api/book', this.httpOptions).subscribe(user => {
this.http.get('/api/getCurrentUser', this.httpOptions).subscribe(user => {
this.currentUser = user;
console.log('mothafuka cureent user : ' + JSON.stringify(this.currentUser));
this.openFolder('Home');
}, err => {
if (err.status === 401) {
this.router.navigate(['login']);
}
});
});
this.getfile();
}
getfile() {
this.http.get('/api/allUserFile', this.httpOptions).subscribe(files => {
this.allUserFile = files;
console.log('mothafuka cureent user : ' + JSON.stringify(this.allUserFile));
}, err => {
if (err.status === 401) {
this.router.navigate(['login']);
getfiles() {
this.http.post('/api/getFileList', {mainPath : this.mainFolder.path, owner: this.currentUser.username.toString()}).subscribe(files => {
if (files) {
this.allUserFile = files;
} 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.tokenFromLogin = '';
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);
});
}
this.makeFileRequest('http://localhost:3000/api/upload', [], this.filesToUpload).then((result) => {
// var source = 'http://localhost:3000/public/'+result[0].filename;
console.log('current USer Filleeee ::: '+ JSON.stringify(this.currentUser));
var fileUploadData = { uid: '222', name:result[0].filename.toString(), path: result[0].path.toString(), extention:this.filesToUpload[0].type.toString(), taille:this.filesToUpload[0].size.toString(), idUser: this.currentUser.id };
var message = '';
//console.log('mothafukaaaaaaaaaaa : ' + JSON.stringify(this.currentUser));
//console.log('mothafukaaaaaaaaaaa : ' + this.currentUser._id);
console.log('avaaaaanttttt : ' + this.currentUser.username.toString() + ' ---- ' + this.currentUser._id.toString());
console.log('lastModifiedDateee : ' + this.filesToUpload[0].lastModifiedDate.toString());
// if(this.currentUser.username)
this.http.post('api/uploadFileMongo', { name: result[0].filename.toString(), path: result[0].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: this.filesToUpload[0].lastModifiedDate.toString() }).subscribe(resp => {
console.log('ok db !!' + resp.toString());
this.getfile();
this.filesToUpload = null;
this.fileChooseName = 'None';
}, err => {
console.log('errrrr : ' + err);
message = err.error.msg;
});
console.log('apres');
console.log('fileUploadData: ' + fileUploadData);
console.log('fileUploadData.path: ' + fileUploadData.path);
console.log('fileUploadData.name: ' + fileUploadData.name);
console.log('fileUploadData.taille: ' + fileUploadData.taille);
console.log('fileUploadData.extention: ' + fileUploadData.extention);
console.log('result: ' + result[0].filename);
}, (error) => {
console.error(error);
});
}
fileChangeEvent(fileInput: any){
@ -105,12 +140,12 @@ export class BookComponent implements OnInit, OnDestroy {
this.fileChooseName = this.filesToUpload[0].name;
}
makeFileRequest(url: string, params: Array<string>, files: Array<File>) {
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], files[0].name);
formData.append("public[]", files[0], monFile._id);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
@ -126,10 +161,4 @@ export class BookComponent implements OnInit, OnDestroy {
});
}
logout() {
this.httpOptions = null;
localStorage.removeItem('jwtToken');
this.router.navigate(['login']);
}
}