From 5a91dda62712f071de698b09efee706d4219a37c Mon Sep 17 00:00:00 2001 From: Tmadkaud Date: Sun, 13 May 2018 18:17:18 +0200 Subject: [PATCH] Grosse mise a jour creation complete du file system dossier et PATH --- models/FolderMongo.js | 51 +++++++++++ routes/api.js | 109 ++++++++++++---------- src/app/book/book.component.html | 32 ++++++- src/app/book/book.component.ts | 149 ++++++++++++++++++------------- 4 files changed, 229 insertions(+), 112 deletions(-) create mode 100644 models/FolderMongo.js diff --git a/models/FolderMongo.js b/models/FolderMongo.js new file mode 100644 index 0000000..ebf0127 --- /dev/null +++ b/models/FolderMongo.js @@ -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); \ No newline at end of file diff --git a/routes/api.js b/routes/api.js index d6b31ac..70995f2 100644 --- a/routes/api.js +++ b/routes/api.js @@ -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.'}); } diff --git a/src/app/book/book.component.html b/src/app/book/book.component.html index 687af46..9035259 100644 --- a/src/app/book/book.component.html +++ b/src/app/book/book.component.html @@ -1,11 +1,29 @@
+
+ +

Path : {{mainFolder.path}}

+

Vous êtes dans le dossier : {{mainFolder.name}}

+ +
+ + + + + + +
+
+ +
+ +
+ -
@@ -19,6 +37,14 @@ + + + + + + + + @@ -26,10 +52,10 @@
- +
- +
diff --git a/src/app/book/book.component.ts b/src/app/book/book.component.ts index faca757..5fd788a 100644 --- a/src/app/book/book.component.ts +++ b/src/app/book/book.component.ts @@ -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; 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, files: Array) { + makeFileRequest(url: string, params: Array, files: Array, 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']); - } - }
{{ folder.name }}{{ folder.taille}} MoFolder{{ folder.owner }}
{{ i + 1 }} {{ file.name }}{{ file.extention }} {{ file.lastDate }}