MediaPathService.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Services;
  3. use App\Util\ActivityPub\Helpers;
  4. use Illuminate\Support\Facades\Redis;
  5. use Illuminate\Support\Str;
  6. use App\Media;
  7. use App\Profile;
  8. use App\User;
  9. use App\Services\HashidService;
  10. class MediaPathService {
  11. public static function get($account, $version = 1)
  12. {
  13. $mh = hash('sha256', date('Y').'-.-'.date('m'));
  14. if($account instanceOf User) {
  15. switch ($version) {
  16. // deprecated
  17. case 1:
  18. $monthHash = hash('sha1', date('Y').date('m'));
  19. $userHash = hash('sha1', $account->id . (string) $account->created_at);
  20. $path = "public/m/{$monthHash}/{$userHash}";
  21. break;
  22. case 2:
  23. $monthHash = substr($mh, 0, 9).'-'.substr($mh, 9, 6);
  24. $userHash = $account->profile_id;
  25. $random = Str::random(12);
  26. $path = "public/m/_v2/{$userHash}/{$monthHash}/{$random}";
  27. break;
  28. default:
  29. $monthHash = substr($mh, 0, 9).'-'.substr($mh, 9, 6);
  30. $userHash = $account->profile_id;
  31. $random = Str::random(12);
  32. $path = "public/m/_v2/{$userHash}/{$monthHash}/{$random}";
  33. break;
  34. }
  35. }
  36. if($account instanceOf Profile) {
  37. $monthHash = substr($mh, 0, 9).'-'.substr($mh, 9, 6);
  38. $userHash = $account->id;
  39. $random = Str::random(12);
  40. $path = "public/m/_v2/{$userHash}/{$monthHash}/{$random}";
  41. }
  42. return $path;
  43. }
  44. public static function story($account, $version = 1)
  45. {
  46. $mh = hash('sha256', date('Y').'-.-'.date('m'));
  47. $monthHash = HashidService::encode(date('Y').date('m'));
  48. $random = date('d').Str::random(32);
  49. if($account instanceOf User) {
  50. switch ($version) {
  51. case 1:
  52. $userHash = HashidService::encode($account->profile_id);
  53. $path = "public/_esm.t3/{$monthHash}/{$userHash}/{$random}";
  54. break;
  55. default:
  56. $userHash = HashidService::encode($account->profile_id);
  57. $path = "public/_esm.t3/{$monthHash}/{$userHash}/{$random}";
  58. break;
  59. }
  60. }
  61. if($account instanceOf Profile) {
  62. $userHash = HashidService::encode($account->id);
  63. $path = "public/_esm.t3/{$monthHash}/{$userHash}/{$random}";
  64. }
  65. return $path;
  66. }
  67. }