Proyectos de Subversion LeadersLinked - Services

Rev

Rev 302 | Rev 324 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
283 www 1
<?php
2
namespace LeadersLinked\Library;
3
 
4
use LeadersLinked\Model\User;
5
use LeadersLinked\Model\UserProfile;
6
use LeadersLinked\Model\Company;
7
use LeadersLinked\Model\Group;
8
 
9
class Storage
10
{
11
    /**
12
     *
13
     * @var\LeadersLinked\Library\Storage
14
     */
15
    private static $_instance;
16
 
17
    /**
18
     *
19
     * @var array
20
     */
21
    private $config;
22
 
23
 
24
    /**
25
     *
26
     * @var S3Files
27
     */
28
    private $s3Files;
29
 
30
    /**
31
     *
32
     * @param array $config
33
     */
34
    private function __construct($config)
35
    {
36
        $this->config = $config;
37
        $this->s3Files = S3Files::getInstance($this->config);
38
    }
39
 
40
    /**
41
     *
42
     * @param array $config
43
     * @return \LeadersLinked\Library\Storage
44
     */
45
    public static function getInstance($config)
46
    {
47
        if(self::$_instance == null) {
48
            self::$_instance = new Storage($config);
49
        }
50
 
51
        return self::$_instance;
52
    }
53
 
54
    /**
55
     *
56
     * @param User $user
57
     * @return string
58
     */
59
    public function getUserImage($user)
60
    {
61
        if($user->image) {
62
            $remoto = $this->getPathUser() . '/' . $user->uuid . '/' .  $user->image;
63
            if($this->s3Files->objectExist($remoto)) {
287 www 64
                return $this->s3Files->getPresignedUrl($remoto);
65
 
283 www 66
            }
67
        }
68
 
69
        $remoto = $this->config['leaderslinked.images_default.user_image'];
70
        return $this->s3Files->getPresignedUrl($remoto);
71
    }
72
 
73
 
74
    /**
75
     *
76
     * @param User $user
77
     * @return string
78
     */
79
    public function getUserImageForCodeAndFilename($code, $filename)
80
    {
81
        if($filename) {
82
            $remoto = $this->getPathUser() . '/' . $code . '/' .  $filename;
83
            if($this->s3Files->objectExist($remoto)) {
287 www 84
                return $this->s3Files->getPresignedUrl($remoto);
85
 
283 www 86
            }
87
        }
88
 
89
        $remoto = $this->config['leaderslinked.images_default.user_image'];
287 www 90
        return $this->s3Files->getPresignedUrl($remoto);
91
 
283 www 92
    }
93
 
94
    /**
95
     *
96
     * @param User $user
97
     * @param UserProfile $userProfile
98
     * @return string
99
     */
100
    public function getUserProfileImage($user, $userProfile)
101
    {
102
        if($userProfile->image) {
103
            $remoto = $this->getPathUser() . '/' . $user->uuid . '/' .  $userProfile->image;
104
            if($this->s3Files->objectExist($remoto)) {
287 www 105
                return $this->s3Files->getPresignedUrl($remoto);
106
 
283 www 107
            }
108
        }
109
 
110
        $remoto = $this->config['leaderslinked.images_default.user_profile'];
287 www 111
        return $this->s3Files->getPresignedUrl($remoto);
112
 
283 www 113
    }
114
 
115
    /**
116
     *
117
     * @param User $user
118
     * @param UserProfile $userProfile
119
     * @return string
120
     */
121
    public function getUserProfileCover($user, $userProfile)
122
    {
123
        if($userProfile->cover) {
124
            $remoto = $this->getPathUser() . '/' . $user->uuid . '/' .  $userProfile->cover;
125
            if($this->s3Files->objectExist($remoto)) {
287 www 126
                return $this->s3Files->getPresignedUrl($remoto);
127
 
283 www 128
            }
129
        }
130
 
131
        $remoto = $this->config['leaderslinked.images_default.user_cover'];
287 www 132
        return $this->s3Files->getPresignedUrl($remoto);
133
 
283 www 134
    }
135
 
136
    /**
137
     *
138
     * @param Company $company
139
     * @return string
140
     */
141
    public function getCompanyImage($company)
142
    {
143
        if($company->image) {
144
            $remoto = $this->getPathCompany() . '/' . $company->uuid . '/' .  $company->image;
145
            if($this->s3Files->objectExist($remoto)) {
287 www 146
                return $this->s3Files->getPresignedUrl($remoto);
147
 
283 www 148
            }
149
        }
150
 
151
        $remoto = $this->config['leaderslinked.images_default.company_profile'];
287 www 152
        return $this->s3Files->getPresignedUrl($remoto);
153
 
283 www 154
    }
155
 
156
    /**
157
     *
158
     * @param string $code
159
     * @param string $filename
160
     * @return string
161
     */
162
    public function getCompanyImageForCodeAndFilename($code, $filename)
163
    {
164
        if($filename) {
165
            $remoto = $this->getPathCompany() . '/' . $code . '/' .  $filename;
166
            if($this->s3Files->objectExist($remoto)) {
287 www 167
                return $this->s3Files->getPresignedUrl($remoto);
168
 
283 www 169
            }
170
        }
171
 
172
        $remoto = $this->config['leaderslinked.images_default.company_profile'];
287 www 173
        return $this->s3Files->getPresignedUrl($remoto);
174
 
283 www 175
    }
176
 
177
    /**
178
     *
179
     * @param Company $company
180
     * @return string
181
     */
182
    public function getCompanyCover($company)
183
    {
184
        if($company->cover) {
185
            $remoto = $this->getPathCompany() . '/' . $company->uuid . '/' .  $company->cover;
186
            if($this->s3Files->objectExist($remoto)) {
287 www 187
                return $this->s3Files->getPresignedUrl($remoto);
188
 
283 www 189
            }
190
        }
191
 
192
        $remoto = $this->config['leaderslinked.images_default.company_cover'];
287 www 193
        return $this->s3Files->getPresignedUrl($remoto);
194
 
283 www 195
    }
196
 
197
    /**
198
     *
199
     * @param Group $group
200
     * @return string
201
     */
202
    public function getGroupImage($group)
203
    {
204
        if($group->image) {
205
            $remoto = $this->getPathGroup() . '/' . $group->uuid . '/' .  $group->image;
206
            if($this->s3Files->objectExist($remoto)) {
287 www 207
                return $this->s3Files->getPresignedUrl($remoto);
208
 
283 www 209
            }
210
        }
211
 
212
        $remoto = $this->config['leaderslinked.images_default.group_profile'];
287 www 213
        return $this->s3Files->getPresignedUrl($remoto);
214
 
283 www 215
    }
216
 
217
    /**
218
     *
219
     * @param string $code
220
     * @param string $filename
221
     * @return string
222
     */
223
    public function getGroupImageForCodeAndFilename($code, $filename)
224
    {
225
        if($filename) {
226
            $remoto = $this->getPathGroup() . '/' . $code . '/' .  $filename;
227
            if($this->s3Files->objectExist($remoto)) {
287 www 228
                return $this->s3Files->getPresignedUrl($remoto);
229
 
283 www 230
            }
231
        }
232
 
233
        $remoto = $this->config['leaderslinked.images_default.group_profile'];
287 www 234
        return $this->s3Files->getPresignedUrl($remoto);
235
 
283 www 236
    }
237
 
238
    /**
239
     *
240
     * @param Group $group
241
     * @return string
242
     */
243
    public function getGroupCover($group)
244
    {
245
        if($group->cover) {
246
            $remoto = $this->getPathGroup() . '/' . $group->uuid . '/' .  $group->cover;
247
            if($this->s3Files->objectExist($remoto)) {
287 www 248
                return $this->s3Files->getPresignedUrl($remoto);
249
 
283 www 250
            }
251
        }
252
 
253
        $remoto = $this->config['leaderslinked.images_default.group_cover'];
287 www 254
        return $this->s3Files->getPresignedUrl($remoto);
255
 
283 www 256
    }
257
 
258
    /**
259
     *
260
     * @param Group $group
261
     * @return string
262
     */
263
    public function getGenericImage($path, $code, $filename)
264
    {
265
        $remoto = $path . '/' . $code. '/' .  $filename;
266
        if($this->s3Files->objectExist($remoto)) {
287 www 267
            return $this->s3Files->getPresignedUrl($remoto);
268
 
283 www 269
        } else {
270
            $remoto = $this->config['leaderslinked.images_default.no_image'];
287 www 271
            return $this->s3Files->getPresignedUrl($remoto);
272
 
283 www 273
        }
274
    }
275
 
276
    /**
277
     *
298 www 278
     * @param string $path
279
     * @param string $code,
280
     * @param string $filename
281
     * @param boolean $checkExists
283 www 282
     * @return string
283
     */
298 www 284
    public function getGenericFile($path, $code, $filename, $checkExists = true)
283 www 285
    {
286
        $remoto = $path . '/' . $code. '/' .  $filename;
298 www 287
 
288
        if($checkExists) {
289
            if($this->s3Files->objectExist($remoto)) {
290
                return $this->s3Files->getPresignedUrl($remoto);
291
 
292
            } else {
293
                return;
294
            }
295
        }
296
        return $this->s3Files->getPresignedUrl($remoto);
283 www 297
    }
298
 
320 www 299
    /**
300
     *
301
     * @param string $path
302
     * @param string $code
303
     * @param string $filename
304
     * @return string
305
     */
306
    public function delete($path, $code, $filename)
307
    {
308
        $remoto = $path . '/' . $code. '/' .  $filename;
309
        try {
310
            $this->s3Files->deleteObject($remoto);
311
            return true;
312
        } catch (\Exception $exception) {
313
            //echo "No se pudo borrar el archivo : $remoto (" . $exception->getMessage() . ")";
314
            return false;
315
        }
316
 
317
    }
318
 
295 www 319
 
320
 
302 www 321
    /**
322
     *
323
     * @return string
324
     */
325
    public function getPathMedia()
326
    {
327
        return $this->config['leaderslinked.minio.fullpath_media'];
328
    }
295 www 329
 
330
 
331
 
283 www 332
    /**
333
     *
334
     * @return string
335
     */
336
    public function getPathGroup()
337
    {
338
        return $this->config['leaderslinked.minio.fullpath_group'];
339
    }
340
 
341
    /**
342
     *
343
     * @return string
344
     */
345
    public function getPathUser()
346
    {
347
        return $this->config['leaderslinked.minio.fullpath_user'];
348
    }
349
 
350
    /**
351
     *
352
     * @return string
353
     */
354
    public function getPathImage()
355
    {
356
        return $this->config['leaderslinked.minio.fullpath_image'];
357
    }
358
 
359
    /**
360
     *
361
     * @return string
362
     */
363
    public function getPathJob()
364
    {
365
        return $this->config['leaderslinked.minio.fullpath_job'];
366
    }
367
 
368
    /**
369
     *
370
     * @return string
371
     */
372
    public function getPathCompany()
373
    {
374
        return $this->config['leaderslinked.minio.fullpath_company'];
375
    }
376
 
377
    /**
378
     *
379
     * @return string
380
     */
381
    public function getPathFeed()
382
    {
383
        return $this->config['leaderslinked.minio.fullpath_feed'];
384
    }
385
 
295 www 386
 
387
 
283 www 388
    /**
389
     *
390
     * @return string
391
     */
295 www 392
    public function getPathMessage()
393
    {
394
        return $this->config['leaderslinked.minio.fullpath_message'];
395
    }
396
 
397
    /**
398
     *
399
     * @return string
400
     */
283 www 401
    public function getPathPost()
402
    {
403
        return $this->config['leaderslinked.minio.fullpath_post'];
404
    }
405
 
406
    /**
407
     *
408
     * @return string
409
     */
410
    public function getPathMicrolearningTopic()
411
    {
412
        return $this->config['leaderslinked.minio.fullpath_microlearning_topic'];
413
    }
414
 
415
    /**
416
     *
417
     * @return string
418
     */
419
    public function getPathMicrolearningCapsule()
420
    {
421
        return $this->config['leaderslinked.minio.fullpath_microlearning_capsule'];
422
    }
423
 
424
    /**
425
     *
426
     * @return string
427
     */
428
    public function getPathMicrolearningSlide()
429
    {
430
        return $this->config['leaderslinked.minio.fullpath_microlearning_slide'];
431
    }
432
 
433
    /**
434
     *
435
     * @return string
436
     */
437
    public function getPathJobDescription()
438
    {
439
        return $this->config['leaderslinked.minio.fullpath_job_description'];
440
    }
441
 
442
    /**
443
     *
444
     * @return string
445
     */
295 www 446
    public function getPathSelfEvaluation()
283 www 447
    {
448
        return $this->config['leaderslinked.minio.fullpath_self_evaluation'];
449
    }
450
 
451
    /**
452
     *
453
     * @return string
454
     */
455
    public function getPathRecruitmentSelection()
456
    {
457
        return $this->config['leaderslinked.minio.fullpath_recruitment_selection'];
458
    }
459
 
460
    /**
461
     *
462
     * @return string
463
     */
464
    public function getPathPerformanceEvaluation()
465
    {
466
        return $this->config['leaderslinked.minio.fullpath_performance_evaluation'];
467
    }
468
 
469
    /**
470
     *
471
     * @return string
472
     */
473
    public function getPathPlanningObjectivesAnGoals()
474
    {
475
        return $this->config['leaderslinked.minio.fullpath_planning_objectives_and_goals'];
476
    }
477
 
478
    /**
479
     *
480
     * @return string
481
     */
482
    public function getPathSurvey()
483
    {
484
        return $this->config['leaderslinked.minio.fullpath_survey'];
485
    }
486
 
487
    /**
488
     *
489
     * @return string
490
     */
491
    public function getPathNetwork()
492
    {
493
        return $this->config['leaderslinked.minio.fullpath_network'];
494
    }
495
 
496
    /**
497
     *
498
     * @return string
499
     */
500
    public function getPathEngagementReward()
501
    {
502
        return $this->config['leaderslinked.minio.fullpath_engagement_reward'];
503
    }
504
 
505
    /**
506
     *
507
     * @return string
508
     */
509
    public function getPathDailyPulse()
510
    {
511
        return $this->config['leaderslinked.minio.fullpath_daily_pulse'];
512
    }
513
 
514
    /**
515
     *
516
     * @return string
517
     */
518
    public function getPathKnowledgeArea()
519
    {
520
        return $this->config['leaderslinked.minio.fullpath_knowledge_area'];
521
    }
522
 
523
    /**
524
     *
525
     * @return string
526
     */
527
    public function getPathMyCoach()
528
    {
529
        return $this->config['leaderslinked.minio.fullpath_my_coach'];
530
    }
531
 
532
    /**
533
     *
534
     * @param String $filename
535
     * return boolean
536
     */
537
    public function objectExist($filename)
538
    {
285 www 539
        return $this->s3Files->objectExist($filename);
283 www 540
    }
541
 
542
    /**
543
     *
302 www 544
     * @param string $remote
545
     * @param string $local
283 www 546
     * @return boolean
547
     */
302 www 548
    public function putObject($remote, $local)
283 www 549
    {
302 www 550
        return $this->s3Files->putObject($remote, $local);
283 www 551
    }
552
 
553
 
554
    /**
555
     *
556
     * @param string $filename
557
 
558
     * @return boolean
559
     */
560
    public function deleteObject($filename)
561
    {
285 www 562
        return $this->s3Files->deleteObject($filename);
283 www 563
    }
564
 
565
 
566
    /**
567
     *
568
     * @param string $path
569
     * @param string $code
570
     * @param string $filename
571
     * @return boolean
572
     */
573
    public function deleteFile($path, $code, $filename)
574
    {
575
        if($code) {
576
            $remoto = $path . '/' . $code. '/' .  $filename;
577
        } else {
578
            $remoto = $path . '/' .  $filename;
579
        }
580
        if($this->s3Files->objectExist($remoto)) {
581
            return $this->s3Files->deleteObject($remoto);
582
        } else {
583
            return true;
584
        }
585
    }
295 www 586
 
587
    /**
588
     *
589
     * @param string $path
590
     * @param string $code
591
     * @return boolean
592
     */
593
    public function deleteDirectory($path, $code)
594
    {
595
        $remoto = $path . '/' . $code;
596
        if($this->s3Files->objectExist($remoto)) {
597
            return $this->s3Files->deleteObject($remoto);
598
        } else {
599
            return true;
600
        }
601
    }
602
 
283 www 603
 
604
 
605
    /**
606
     *
607
     * @param string $path
608
     * @param string $code
290 www 609
     * @param string $local_filename
283 www 610
     * @return boolean
611
     */
290 www 612
    public function putFile($path, $code, $local_filename)
283 www 613
    {
614
        if($code) {
290 www 615
            $remote = $path . '/' . $code . '/' . basename($local_filename);
283 www 616
        } else {
290 www 617
            $remote = $path. '/' . basename($local_filename);
283 www 618
        }
619
 
290 www 620
 
621
        $result = $this->s3Files->putObject($remote, $local_filename);
622
        if(file_exists($local_filename)) {
623
            @unlink($local_filename);
624
        }
283 www 625
 
626
        return $result;
627
    }
628
 
629
 
630
    /**
631
     *
632
     * @param string $path
633
     * @param string $code
634
     * @param string $tempfile
635
     * @param string $filename
636
     * @return boolean
637
     */
638
    public function moveAndPutFile($path, $code, $tempfile, $filename)
639
    {
640
        if($code) {
641
            $filepath = $path . '/' . $code;
642
        } else {
643
            $filepath = $path;
644
        }
645
 
646
        $filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $filename;
647
 
648
        move_uploaded_file($tempfile, $filename);
649
 
285 www 650
        $result = $this->s3Files->putObject($filename, $filepath);
283 www 651
        @unlink($filename);
652
 
653
        return $result;
654
    }
655
 
656
    /**
657
     *
658
     * @return string
659
     */
660
    public function getTempPath()
661
    {
662
        return 'data' . DIRECTORY_SEPARATOR . 'storage';
663
    }
664
 
665
 
666
 
667
 
668
 
669
 
670
}