MediaPathService.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. class MediaPathService {
  10. public static function get($account, $version = 1)
  11. {
  12. $mh = hash('sha256', date('Y').'-.-'.date('m'));
  13. if($account instanceOf User) {
  14. switch ($version) {
  15. // deprecated
  16. case 1:
  17. $monthHash = hash('sha1', date('Y').date('m'));
  18. $userHash = hash('sha1', $account->id . (string) $account->created_at);
  19. $path = "public/m/{$monthHash}/{$userHash}";
  20. break;
  21. case 2:
  22. $monthHash = substr($mh, 0, 9).'-'.substr($mh, 9, 6);
  23. $userHash = $account->profile_id;
  24. $random = Str::random(12);
  25. $path = "public/m/_v2/{$userHash}/{$monthHash}/{$random}";
  26. break;
  27. default:
  28. $monthHash = substr($mh, 0, 9).'-'.substr($mh, 9, 6);
  29. $userHash = $account->profile_id;
  30. $random = Str::random(12);
  31. $path = "public/m/_v2/{$userHash}/{$monthHash}/{$random}";
  32. break;
  33. }
  34. }
  35. if($account instanceOf Profile) {
  36. $monthHash = substr($mh, 0, 9).'-'.substr($mh, 9, 6);
  37. $userHash = $account->id;
  38. $random = Str::random(12);
  39. $path = "public/m/_v2/{$userHash}/{$monthHash}/{$random}";
  40. }
  41. return $path;
  42. }
  43. public static function story($account, $version = 1)
  44. {
  45. $mh = hash('sha256', date('Y').'-.-'.date('m'));
  46. $monthHash = date('Y').date('m').substr($mh, 0, 6).substr($mh, 58, 6);
  47. $random = '03'.Str::random(random_int(6,9)).'_'.Str::random(random_int(6,17));
  48. if($account instanceOf User) {
  49. switch ($version) {
  50. case 1:
  51. $userHash = $account->profile_id;
  52. $path = "public/_esm.t3/{$monthHash}/{$userHash}/{$random}";
  53. break;
  54. default:
  55. $userHash = $account->profile_id;
  56. $path = "public/_esm.t3/{$monthHash}/{$userHash}/{$random}";
  57. break;
  58. }
  59. }
  60. if($account instanceOf Profile) {
  61. $userHash = $account->id;
  62. $path = "public/_esm.t3/{$monthHash}/{$userHash}/{$random}";
  63. }
  64. return $path;
  65. }
  66. }