elderwand 0.1.71 → 0.1.72

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,7 @@
7
7
  var mv = require('mv');
8
8
  var fs = require('fs');
9
9
  var path = require('path');
10
+ const archiver = require('archiver');
10
11
  //var utils = rekuire('/lib/utils');
11
12
  var extend = require('util')._extend;
12
13
  var ObjectId = require('mongoose').Types.ObjectId
@@ -24,7 +25,7 @@ module.exports.inherit = function (child) {
24
25
  child.super = {};
25
26
  child.elements = []
26
27
  child.coveredMethods = [];
27
- child.coveredMethods.push("load");
28
+ child.coveredMethods.push("load");
28
29
  child.load = function (req, res, next, id) {
29
30
  if(ObjectId.isValid(id)){
30
31
  child.model.load(id, function (err, model) {
@@ -122,7 +123,7 @@ module.exports.inherit = function (child) {
122
123
 
123
124
  var page = parseInt(req.anyKey.page > 0 ? req.anyKey.page : 1) - 1;
124
125
  var perPage = parseInt(req.anyKey.perPage > 0 ? req.anyKey.perPage : 10);
125
- var text = req.anyKey.q
126
+ var text = req.anyKey.q
126
127
  var options = {
127
128
  perPage: perPage,
128
129
  page: page,
@@ -134,7 +135,7 @@ module.exports.inherit = function (child) {
134
135
  if(child.model.generateCriteria){
135
136
  options.criteria.inject(child.model.generateCriteria(req.anyKey))
136
137
  }
137
-
138
+
138
139
  if(child.model.listByUserOptions){
139
140
  options.criteria = child.model.listByUserOptions(options, req.user, callback)
140
141
  }else{
@@ -245,7 +246,7 @@ module.exports.inherit = function (child) {
245
246
  res.statusData = "Error"
246
247
  res.redirect('/login/');
247
248
  }
248
- })
249
+ })
249
250
  });
250
251
  } else {
251
252
  //EWLog("yetki", child.singularName, req[child.singularName])
@@ -260,7 +261,7 @@ module.exports.inherit = function (child) {
260
261
  }
261
262
  };
262
263
 
263
- /**
264
+ /**
264
265
  * Edit an article
265
266
  */
266
267
 
@@ -340,7 +341,7 @@ module.exports.inherit = function (child) {
340
341
  if(err){
341
342
  res.errors.push(err)
342
343
  req.flash('error', err.message);
343
- }
344
+ }
344
345
  if(error){
345
346
  res.errors.push(error)
346
347
  req.flash('error', error.message);
@@ -468,7 +469,7 @@ module.exports.inherit = function (child) {
468
469
  }
469
470
  var page = parseInt(req.anyKey.page > 0 ? req.anyKey.page : 1) - 1;
470
471
  var perPage = parseInt(req.anyKey.perPage > 0 ? req.anyKey.perPage : 10);
471
- var text = req.anyKey.q
472
+ var text = req.anyKey.q
472
473
  var options = {
473
474
  perPage: perPage,
474
475
  page: page,
@@ -480,7 +481,7 @@ module.exports.inherit = function (child) {
480
481
  var ids = req.anyKey.ids
481
482
  if(ids.indexOf("all") == -1){
482
483
  options["criteria"] = {_id: {$in: ids}}
483
- }
484
+ }
484
485
  var criteria = {}
485
486
  if(child.model.listByUserOptions){
486
487
  criteria = child.model.listByUserOptions(options, req.user, callback)
@@ -512,7 +513,7 @@ module.exports.inherit = function (child) {
512
513
  if(req.csrfToken){
513
514
  data.csrf_token = req.csrfToken()
514
515
  }
515
- callback(data);
516
+ callback(data);
516
517
  };
517
518
  child.super.loadRelatedData = child.loadRelatedData
518
519
  child.coveredMethods.push("authFilter");
@@ -544,13 +545,57 @@ module.exports.inherit = function (child) {
544
545
 
545
546
  };
546
547
  child.coveredMethods.push("lookFile");
547
- child.lookFile = function(req, res){
548
+ child.lookFile = function(req, res){
548
549
  if(req[child.singularName] && req[child.singularName].canSeeByUser(req.user)){
549
550
  res.sendFile(path.resolve('uploads/' + child.singularName + "/" + req[child.singularName]['id'] + '/' + req.params.fileName))
550
- }else{
551
+ }else{
551
552
  res.json({error: "You are not authorized"})
552
553
  }
553
554
  }
555
+ child.coveredMethods.push("lookMultipleFiles");
556
+ child.lookMultipleFiles = function(req, res) {
557
+ if (req[child.singularName] && req[child.singularName].canSeeByUser(req.user)) {
558
+ const zipNames = req.params.zipName.split(',').map(name => name.trim());
559
+ const id = req[child.singularName]._id.toString();
560
+ const uploadPath = path.resolve('uploads', child.singularName, id.toString());
561
+ const zipFileName = `${id}.zip`;
562
+ const zipFilePath = path.join(uploadPath, zipFileName);
563
+ const output = fs.createWriteStream(zipFilePath);
564
+ const archive = archiver('zip');
565
+
566
+ output.on('close', function() {
567
+ console.log(`Zip dosyası oluşturuldu: ${zipFilePath} (${archive.pointer()} toplam bayt)`);
568
+ res.download(zipFilePath, function(err) {
569
+ if (err) {
570
+ console.error('İndirme işlemi başarısız:', err);
571
+ }
572
+ fs.unlink(zipFilePath, (err) => {
573
+ if (err) console.error('Zip dosyası silinemedi:', err);
574
+ });
575
+ });
576
+ });
577
+
578
+ archive.on('error', function(err) {
579
+ console.error('Arşivleme hatası:', err);
580
+ return res.status(500).json({ error: 'Zipping failed' });
581
+ });
582
+
583
+ archive.pipe(output);
584
+
585
+ zipNames.forEach(file => {
586
+ const filePath = path.join(uploadPath, file);
587
+ if (fs.existsSync(filePath)) {
588
+ archive.file(filePath, { name: file });
589
+ } else {
590
+ console.warn(`Dosya mevcut değil: ${filePath}`);
591
+ }
592
+ });
593
+
594
+ archive.finalize();
595
+ } else {
596
+ res.json({ error: "Yetkiniz yok" });
597
+ }
598
+ };
554
599
 
555
600
  child.coveredMethods.push("uploadFile");
556
601
  child.uploadFile = function(req, res){
@@ -561,17 +606,17 @@ module.exports.inherit = function (child) {
561
606
  res.json({error:error, err:err})
562
607
  }
563
608
  })
564
- }
609
+ }
565
610
  child.coveredMethods.push("handleFiles");
566
611
  child.handleFiles = function(req, res, next){
567
612
  if(req[child.singularName] && req[child.singularName].canEditByUser(req.user)){
568
613
  if (!fs.existsSync(path.resolve('uploads/' + child.singularName))){
569
614
  fs.mkdirSync(path.resolve('uploads/' + child.singularName));
570
- }
615
+ }
571
616
  if (!fs.existsSync(path.resolve('uploads/' + child.singularName + "/" + req[child.singularName].id))){
572
617
  fs.mkdirSync(path.resolve('uploads/' + child.singularName + "/" + req[child.singularName].id));
573
- }
574
- var needToSave = false
618
+ }
619
+ var needToSave = false
575
620
 
576
621
  if(req.files == null){
577
622
  req.files = {}
@@ -586,7 +631,7 @@ module.exports.inherit = function (child) {
586
631
  req[child.singularName].walk(key, [])
587
632
  }
588
633
  async.each(req.files[key], function(file, tick){
589
- file["dataPath"] = path.resolve('uploads/' + child.singularName + "/" + req[child.singularName].id + '/' + file.name)
634
+ file["dataPath"] = path.resolve('uploads/' + child.singularName + "/" + req[child.singularName].id + '/' + file.name)
590
635
  var item = req[child.singularName].walk(key);
591
636
  item.push(file.name)
592
637
  mv(file.newPath, file.dataPath, tick)
@@ -595,18 +640,18 @@ module.exports.inherit = function (child) {
595
640
  if(req[child.singularName].walk(key) == undefined || req[child.singularName].walk(key) == null){
596
641
  req[child.singularName].walk(key, {})
597
642
  }
598
- req.files[key]["dataPath"] = path.resolve('uploads/' + child.singularName + "/" + req[child.singularName].id + '/' + req.files[key].name)
643
+ req.files[key]["dataPath"] = path.resolve('uploads/' + child.singularName + "/" + req[child.singularName].id + '/' + req.files[key].name)
599
644
  req[child.singularName].walk(key, req.files[key].name);
600
645
  mv(req.files[key].newPath, req.files[key].dataPath, next)
601
646
  }
602
647
  }, function(err){
603
648
  //EWLog(needToSave)
604
649
  if(needToSave){
605
- req[child.singularName].save(next)
650
+ req[child.singularName].save(next)
606
651
  }else{
607
652
  next()
608
653
  }
609
- })
654
+ })
610
655
  }else{
611
656
  req.flash('info', 'Bu işlemi yapmak için yetkiniz yok');
612
657
  if(req.user){
@@ -617,7 +662,7 @@ module.exports.inherit = function (child) {
617
662
  return res.redirect('/login');
618
663
  }
619
664
  }
620
- }
665
+ }
621
666
  child.apify = function(funcName){
622
667
  var method = function(req, res){
623
668
  var argsPre = arguments.toArrayEW();
@@ -625,13 +670,13 @@ module.exports.inherit = function (child) {
625
670
  res.redirect = function(name){
626
671
  var data = {
627
672
  status: res.statusData,
628
- type:"redirect",
673
+ type:"redirect",
629
674
  name:name,
630
675
  model:res.model,
631
676
  flash:res.flashData
632
677
  }
633
678
  data[child.singularName] = res.model
634
- res.json(data);
679
+ res.json(data);
635
680
  }
636
681
  res._render = res.redirect
637
682
  res.render = function(name, model){
@@ -643,21 +688,21 @@ module.exports.inherit = function (child) {
643
688
  flash:res.flashData
644
689
  }
645
690
  data[child.singularName] = res.model
646
- res.json(data);
691
+ res.json(data);
647
692
  }
648
693
  res._flash = res.flash
649
694
  res.flash = function(name, message){
650
695
  if(res.flashData == undefined || res.flashData == null){
651
696
  res.flashData = []
652
697
  }
653
- res.flashData.push({name:name, message:message});
698
+ res.flashData.push({name:name, message:message});
654
699
  }
655
700
  req._flash = req.flash
656
701
  req.flash = function(name, message){
657
702
  if(res.flashData == undefined || res.flashData == null){
658
703
  res.flashData = []
659
704
  }
660
- res.flashData.push({name:name, message:message});
705
+ res.flashData.push({name:name, message:message});
661
706
  }
662
707
  child[funcName].apply(this, argsPre);
663
708
  }
@@ -670,7 +715,7 @@ module.exports.inherit = function (child) {
670
715
  }else if(req.body[child.singularName + 'Id']){
671
716
  child.load(req, res, function(){
672
717
  method.apply(this, argsInner)
673
- }, req.body[child.singularName + 'Id'])
718
+ }, req.body[child.singularName + 'Id'])
674
719
  }else if(req.query[child.singularName + 'Id']){
675
720
  child.load(req, res, function(){
676
721
  method.apply(this, argsInner)
@@ -691,7 +736,7 @@ module.exports.inherit = function (child) {
691
736
  }
692
737
  }
693
738
  }
694
- };
739
+ };
695
740
  child.apifyAllRequests();*/
696
741
 
697
742
 
@@ -707,13 +752,13 @@ module.exports.inherit = function (child) {
707
752
  res.redirect = function(name){
708
753
  var data = {
709
754
  status: res.statusData,
710
- type:"redirect",
755
+ type:"redirect",
711
756
  name:name,
712
757
  model:res.model,
713
758
  flash:res.flashData
714
759
  }
715
760
  data[child.singularName] = res.model
716
- res.json(data);
761
+ res.json(data);
717
762
  }
718
763
  res._render = res.redirect
719
764
  res.render = function(name, model){
@@ -725,21 +770,21 @@ module.exports.inherit = function (child) {
725
770
  flash:res.flashData
726
771
  }
727
772
  data[child.singularName] = res.model
728
- res.json(data);
773
+ res.json(data);
729
774
  }
730
775
  res._flash = res.flash
731
776
  res.flash = function(name, message){
732
777
  if(res.flashData == undefined || res.flashData == null){
733
778
  res.flashData = []
734
779
  }
735
- res.flashData.push({name:name, message:message});
780
+ res.flashData.push({name:name, message:message});
736
781
  }
737
782
  req._flash = req.flash
738
783
  req.flash = function(name, message){
739
784
  if(res.flashData == undefined || res.flashData == null){
740
785
  res.flashData = []
741
786
  }
742
- res.flashData.push({name:name, message:message});
787
+ res.flashData.push({name:name, message:message});
743
788
  }
744
789
  }
745
790
 
@@ -756,12 +801,12 @@ module.exports.inherit = function (child) {
756
801
  var params = getParamNames(obj);
757
802
  if(params[0] == "req" && params[1] == "res" && params.length == 2){
758
803
  //EWLog(child.pluralName, key)
759
- this.apify(key);
804
+ this.apify(key);
760
805
  this.acceptCheck(key);
761
806
  }
762
807
  }
763
808
  }
764
- };
809
+ };
765
810
  child.apifyAllRequests = child.apifyAllRequests
766
811
  };
767
812
  var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elderwand",
3
- "version": "0.1.71",
3
+ "version": "0.1.72",
4
4
  "description": "Mamtas Technology Internal Framework",
5
5
  "main": "./node_modules/.bin/nodemon index.js",
6
6
  "scripts": {
@@ -262,13 +262,18 @@ exports.createWorker = function(path, usageLimit, initPath){
262
262
  worker.__postMessage({type:"INIT", path:initPath});
263
263
  return worker;
264
264
  };
265
- exports.checkWorkQueue = function(worker){
265
+ exports.checkWorkQueue = function(worker, q){
266
266
  if(worker && worker.usageLimit > 0 && worker.used > worker.usageLimit){
267
267
  //console.log("terminated overly used call");
268
268
  worker.terminate();
269
269
  delete worker;
270
270
  return exports.checkWorkQueue();
271
271
  }
272
+ if(q){
273
+ q = q + 1
274
+ }else{
275
+ q = 1
276
+ }
272
277
  var result = false;
273
278
  //EWLog(workers.length, jobQueue.length)
274
279
  if(jobQueue.length > 0){
@@ -301,8 +306,10 @@ exports.checkWorkQueue = function(worker){
301
306
  }
302
307
  }
303
308
 
304
- if(jobQueue.length > 0 && workers.length < MAX_WORKING_THREADS){
305
- exports.checkWorkQueue(worker);
309
+ if(jobQueue.length > 0 &&
310
+ workers.length < MAX_WORKING_THREADS &&
311
+ q < 5){
312
+ exports.checkWorkQueue(worker, q);
306
313
  //EWLog(">>>>", workers.length, jobQueue.length)
307
314
  }
308
315
  //console.log("jobQueue.length", jobQueue.length, workers.length);