elderwand 0.1.71 → 0.1.73

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,56 @@ 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
+ res.download(zipFilePath, function(err) {
568
+ if (err) {
569
+ console.error('İndirme işlemi başarısız:', err);
570
+ }
571
+ fs.unlink(zipFilePath, (err) => {
572
+ if (err) console.error('Zip dosyası silinemedi:', err);
573
+ });
574
+ });
575
+ });
576
+
577
+ archive.on('error', function(err) {
578
+ console.error('Arşivleme hatası:', err);
579
+ return res.status(500).json({ error: 'Zipping failed' });
580
+ });
581
+
582
+ archive.pipe(output);
583
+
584
+ zipNames.forEach(file => {
585
+ const filePath = path.join(uploadPath, file);
586
+ if (fs.existsSync(filePath)) {
587
+ archive.file(filePath, { name: file });
588
+ } else {
589
+ console.warn(`Dosya mevcut değil: ${filePath}`);
590
+ }
591
+ });
592
+
593
+ archive.finalize();
594
+ } else {
595
+ res.json({ error: "Yetkiniz yok" });
596
+ }
597
+ };
554
598
 
555
599
  child.coveredMethods.push("uploadFile");
556
600
  child.uploadFile = function(req, res){
@@ -561,17 +605,17 @@ module.exports.inherit = function (child) {
561
605
  res.json({error:error, err:err})
562
606
  }
563
607
  })
564
- }
608
+ }
565
609
  child.coveredMethods.push("handleFiles");
566
610
  child.handleFiles = function(req, res, next){
567
611
  if(req[child.singularName] && req[child.singularName].canEditByUser(req.user)){
568
612
  if (!fs.existsSync(path.resolve('uploads/' + child.singularName))){
569
613
  fs.mkdirSync(path.resolve('uploads/' + child.singularName));
570
- }
614
+ }
571
615
  if (!fs.existsSync(path.resolve('uploads/' + child.singularName + "/" + req[child.singularName].id))){
572
616
  fs.mkdirSync(path.resolve('uploads/' + child.singularName + "/" + req[child.singularName].id));
573
- }
574
- var needToSave = false
617
+ }
618
+ var needToSave = false
575
619
 
576
620
  if(req.files == null){
577
621
  req.files = {}
@@ -586,7 +630,7 @@ module.exports.inherit = function (child) {
586
630
  req[child.singularName].walk(key, [])
587
631
  }
588
632
  async.each(req.files[key], function(file, tick){
589
- file["dataPath"] = path.resolve('uploads/' + child.singularName + "/" + req[child.singularName].id + '/' + file.name)
633
+ file["dataPath"] = path.resolve('uploads/' + child.singularName + "/" + req[child.singularName].id + '/' + file.name)
590
634
  var item = req[child.singularName].walk(key);
591
635
  item.push(file.name)
592
636
  mv(file.newPath, file.dataPath, tick)
@@ -595,18 +639,18 @@ module.exports.inherit = function (child) {
595
639
  if(req[child.singularName].walk(key) == undefined || req[child.singularName].walk(key) == null){
596
640
  req[child.singularName].walk(key, {})
597
641
  }
598
- req.files[key]["dataPath"] = path.resolve('uploads/' + child.singularName + "/" + req[child.singularName].id + '/' + req.files[key].name)
642
+ req.files[key]["dataPath"] = path.resolve('uploads/' + child.singularName + "/" + req[child.singularName].id + '/' + req.files[key].name)
599
643
  req[child.singularName].walk(key, req.files[key].name);
600
644
  mv(req.files[key].newPath, req.files[key].dataPath, next)
601
645
  }
602
646
  }, function(err){
603
647
  //EWLog(needToSave)
604
648
  if(needToSave){
605
- req[child.singularName].save(next)
649
+ req[child.singularName].save(next)
606
650
  }else{
607
651
  next()
608
652
  }
609
- })
653
+ })
610
654
  }else{
611
655
  req.flash('info', 'Bu işlemi yapmak için yetkiniz yok');
612
656
  if(req.user){
@@ -617,7 +661,7 @@ module.exports.inherit = function (child) {
617
661
  return res.redirect('/login');
618
662
  }
619
663
  }
620
- }
664
+ }
621
665
  child.apify = function(funcName){
622
666
  var method = function(req, res){
623
667
  var argsPre = arguments.toArrayEW();
@@ -625,13 +669,13 @@ module.exports.inherit = function (child) {
625
669
  res.redirect = function(name){
626
670
  var data = {
627
671
  status: res.statusData,
628
- type:"redirect",
672
+ type:"redirect",
629
673
  name:name,
630
674
  model:res.model,
631
675
  flash:res.flashData
632
676
  }
633
677
  data[child.singularName] = res.model
634
- res.json(data);
678
+ res.json(data);
635
679
  }
636
680
  res._render = res.redirect
637
681
  res.render = function(name, model){
@@ -643,21 +687,21 @@ module.exports.inherit = function (child) {
643
687
  flash:res.flashData
644
688
  }
645
689
  data[child.singularName] = res.model
646
- res.json(data);
690
+ res.json(data);
647
691
  }
648
692
  res._flash = res.flash
649
693
  res.flash = function(name, message){
650
694
  if(res.flashData == undefined || res.flashData == null){
651
695
  res.flashData = []
652
696
  }
653
- res.flashData.push({name:name, message:message});
697
+ res.flashData.push({name:name, message:message});
654
698
  }
655
699
  req._flash = req.flash
656
700
  req.flash = function(name, message){
657
701
  if(res.flashData == undefined || res.flashData == null){
658
702
  res.flashData = []
659
703
  }
660
- res.flashData.push({name:name, message:message});
704
+ res.flashData.push({name:name, message:message});
661
705
  }
662
706
  child[funcName].apply(this, argsPre);
663
707
  }
@@ -670,7 +714,7 @@ module.exports.inherit = function (child) {
670
714
  }else if(req.body[child.singularName + 'Id']){
671
715
  child.load(req, res, function(){
672
716
  method.apply(this, argsInner)
673
- }, req.body[child.singularName + 'Id'])
717
+ }, req.body[child.singularName + 'Id'])
674
718
  }else if(req.query[child.singularName + 'Id']){
675
719
  child.load(req, res, function(){
676
720
  method.apply(this, argsInner)
@@ -691,7 +735,7 @@ module.exports.inherit = function (child) {
691
735
  }
692
736
  }
693
737
  }
694
- };
738
+ };
695
739
  child.apifyAllRequests();*/
696
740
 
697
741
 
@@ -707,13 +751,13 @@ module.exports.inherit = function (child) {
707
751
  res.redirect = function(name){
708
752
  var data = {
709
753
  status: res.statusData,
710
- type:"redirect",
754
+ type:"redirect",
711
755
  name:name,
712
756
  model:res.model,
713
757
  flash:res.flashData
714
758
  }
715
759
  data[child.singularName] = res.model
716
- res.json(data);
760
+ res.json(data);
717
761
  }
718
762
  res._render = res.redirect
719
763
  res.render = function(name, model){
@@ -725,21 +769,21 @@ module.exports.inherit = function (child) {
725
769
  flash:res.flashData
726
770
  }
727
771
  data[child.singularName] = res.model
728
- res.json(data);
772
+ res.json(data);
729
773
  }
730
774
  res._flash = res.flash
731
775
  res.flash = function(name, message){
732
776
  if(res.flashData == undefined || res.flashData == null){
733
777
  res.flashData = []
734
778
  }
735
- res.flashData.push({name:name, message:message});
779
+ res.flashData.push({name:name, message:message});
736
780
  }
737
781
  req._flash = req.flash
738
782
  req.flash = function(name, message){
739
783
  if(res.flashData == undefined || res.flashData == null){
740
784
  res.flashData = []
741
785
  }
742
- res.flashData.push({name:name, message:message});
786
+ res.flashData.push({name:name, message:message});
743
787
  }
744
788
  }
745
789
 
@@ -756,12 +800,12 @@ module.exports.inherit = function (child) {
756
800
  var params = getParamNames(obj);
757
801
  if(params[0] == "req" && params[1] == "res" && params.length == 2){
758
802
  //EWLog(child.pluralName, key)
759
- this.apify(key);
803
+ this.apify(key);
760
804
  this.acceptCheck(key);
761
805
  }
762
806
  }
763
807
  }
764
- };
808
+ };
765
809
  child.apifyAllRequests = child.apifyAllRequests
766
810
  };
767
811
  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.73",
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);