src/Entity/MemberMedia.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Dictionary\MemberMediaType;
  4. use App\Service\Media\MemberMediaService;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Doctrine\ORM\Mapping\JoinColumn;
  7. use Doctrine\ORM\Mapping\ManyToOne;
  8. use JMS\Serializer\Annotation as Serializer;
  9. use JMS\Serializer\Annotation\Exclude;
  10. use JMS\Serializer\Annotation\Groups;
  11. /**
  12.  * @ORM\Table("MemberMedia")
  13.  *
  14.  * @ORM\HasLifecycleCallbacks()
  15.  *
  16.  * @ORM\Entity(repositoryClass="App\Repository\MemberMediaRepository")
  17.  */
  18. class MemberMedia
  19. {
  20.     // E.g. https://assets.frivol.com/mediacache/media/__filter__/membermedia/4a/d0/4a/9a3aedaa11a8.jpg
  21.     public const URL_MEDIACACHE_PATTERN 'mediacache/media/__filter__/membermedia/__path__/__encryptedfilename__';
  22.     /**
  23.      * @Serializer\Type("integer")
  24.      *
  25.      * @Groups({"photocheck", "mediaindex", "actorlist", "groupdetails", "membermainphoto", "membermediaurl", "amateur_search"})
  26.      *
  27.      * @ORM\Id()
  28.      *
  29.      * @ORM\GeneratedValue()
  30.      *
  31.      * @ORM\Column(type="integer")
  32.      */
  33.     private $id;
  34.     /**
  35.      * @Groups({"photocheck", "mediaindex", "actorlist", "groupdetails"})
  36.      *
  37.      * @ORM\Column(type="string", length=255)
  38.      */
  39.     private $file_name;
  40.     /**
  41.      * @Groups({"photocheck", "mediaindex", "actorlist", "groupdetails"})
  42.      *
  43.      * @ORM\Column(type="string", length=255)
  44.      */
  45.     private ?string $file_path null;
  46.     /**
  47.      * @Groups({"photocheck", "mediaindex", "membermediaflags"})
  48.      *
  49.      * @ORM\Column(type="boolean", nullable=true)
  50.      */
  51.     private ?bool $is_checked false;
  52.     /**
  53.      * @Groups({"photocheck", "mediaindex", "membermediaflags"})
  54.      *
  55.      * @ORM\Column(type="boolean", nullable=true)
  56.      */
  57.     private ?bool $is_hardcore null;
  58.     /**
  59.      * @ORM\Column(type="datetime")
  60.      */
  61.     private $uploaded_at;
  62.     /**
  63.      * @Exclude()
  64.      *
  65.      * @ORM\Column(type="string", length=32, nullable=true)
  66.      */
  67.     private ?string $import_hash null;
  68.     /**
  69.      * @Groups({"photocheck", "mediaindex"})
  70.      *
  71.      * @ManyToOne(targetEntity="App\Entity\Member",cascade={"persist"})
  72.      *
  73.      * @JoinColumn(name="member_id", referencedColumnName="id", onDelete="SET NULL")
  74.      */
  75.     private ?Member $member null;
  76.     /**
  77.      * @Groups({"photocheck", "mediaindex", "membermediaflags"})
  78.      *
  79.      * @ORM\Column(type="boolean", nullable=true)
  80.      */
  81.     private ?bool $is_proof false;
  82.     /**
  83.      * @Groups({"membermainphoto", "membermediaflags"})
  84.      *
  85.      * @Serializer\Accessor(getter="getIsMain")
  86.      */
  87.     private ?bool $is_main false;
  88.     /**
  89.      * @Groups({"membermediaflags"})
  90.      *
  91.      * @ORM\Column(type="smallint")
  92.      */
  93.     private int $media_type MemberMediaType::IMAGE;
  94.     /**
  95.      * @Groups({"mediaindex", "membermainphoto"})
  96.      *
  97.      * @ORM\Column(type="smallint")
  98.      */
  99.     private $position 0;
  100.     /**
  101.      * @Exclude()
  102.      *
  103.      * @ORM\Column(type="string", length=255, nullable=true)
  104.      */
  105.     private $url;
  106.     /**
  107.      * @Exclude()
  108.      *
  109.      * @ORM\Column(type="string", length=255, nullable=true)
  110.      */
  111.     private $pixelatedUrl;
  112.     /**
  113.      * @ORM\PrePersist()
  114.      */
  115.     public function prePersist()
  116.     {
  117.         if (!$this->getUploadedAt()) {
  118.             $this->setUploadedAt(new \DateTime());
  119.         }
  120.         if (!$this->getFileName()) {
  121.             $this->setFileName($this->generateFileName());
  122.         }
  123.         if (!$this->getFilePath()) {
  124.             $this->setFilePath($this->generateFilePath());
  125.         }
  126.     }
  127.     public function getIsMain(): bool
  128.     {
  129.         $member $this->getMember();
  130.         if (!$member) {
  131.             return false;
  132.         }
  133.         if ($mainPhoto $member->getMainPhoto()) {
  134.             return $mainPhoto->getId() === $this->getId();
  135.         }
  136.         return false;
  137.     }
  138.     public function generateFilePath(): string
  139.     {
  140.         $path md5(mt_rand(11111999999999).microtime(true));
  141.         return implode('/', [
  142.             substr($path02),
  143.             substr($path22),
  144.             substr($path42),
  145.         ]);
  146.     }
  147.     public function generateFileName(): string
  148.     {
  149.         $md5 md5(mt_rand(11111999999999).microtime(true));
  150.         $extension MemberMediaType::IMAGE == $this->getMediaType() ? '.jpg' '.mp4';
  151.         return substr($md508).$extension;
  152.     }
  153.     public function getId()
  154.     {
  155.         return $this->id;
  156.     }
  157.     public function setUrl(?string $url): self
  158.     {
  159.         $this->url $url;
  160.         return $this;
  161.     }
  162.     public function setPixelatedUrl(?string $url): self
  163.     {
  164.         $this->pixelatedUrl $url;
  165.         return $this;
  166.     }
  167.     public function getPosition(): int
  168.     {
  169.         return $this->position;
  170.     }
  171.     /**
  172.      * @return $this
  173.      */
  174.     public function setPosition(int $position): self
  175.     {
  176.         $this->position $position;
  177.         return $this;
  178.     }
  179.     public function getFileName(): ?string
  180.     {
  181.         return $this->file_name;
  182.     }
  183.     public function setFileName(string $file_name): self
  184.     {
  185.         $this->file_name $file_name;
  186.         return $this;
  187.     }
  188.     public function getFilePath(): ?string
  189.     {
  190.         return $this->file_path;
  191.     }
  192.     public function setFilePath(string $file_path): self
  193.     {
  194.         $this->file_path $file_path;
  195.         return $this;
  196.     }
  197.     public function getIsChecked(): ?bool
  198.     {
  199.         return $this->is_checked;
  200.     }
  201.     public function setIsChecked(?bool $is_checked): self
  202.     {
  203.         $this->is_checked $is_checked;
  204.         return $this;
  205.     }
  206.     public function getIsHardcore(): ?bool
  207.     {
  208.         return $this->is_hardcore;
  209.     }
  210.     public function setIsHardcore(?bool $is_hardcore): self
  211.     {
  212.         $this->is_hardcore $is_hardcore;
  213.         return $this;
  214.     }
  215.     public function getUploadedAt(): ?\DateTimeInterface
  216.     {
  217.         return $this->uploaded_at;
  218.     }
  219.     public function setUploadedAt(\DateTimeInterface $uploaded_at): self
  220.     {
  221.         $this->uploaded_at $uploaded_at;
  222.         return $this;
  223.     }
  224.     public function getImportHash(): ?string
  225.     {
  226.         return $this->import_hash;
  227.     }
  228.     public function setImportHash(?string $import_hash): self
  229.     {
  230.         $this->import_hash $import_hash;
  231.         return $this;
  232.     }
  233.     public function getMember(): ?Member
  234.     {
  235.         return $this->member;
  236.     }
  237.     public function setMember(?Member $member): self
  238.     {
  239.         $this->member $member;
  240.         return $this;
  241.     }
  242.     public function getIsProof(): ?bool
  243.     {
  244.         return $this->is_proof;
  245.     }
  246.     public function setIsProof(?bool $is_proof): self
  247.     {
  248.         $this->is_proof $is_proof;
  249.         return $this;
  250.     }
  251.     public function getMediaType(): int
  252.     {
  253.         return $this->media_type;
  254.     }
  255.     public function setMediaType(int $media_type): self
  256.     {
  257.         $this->media_type $media_type;
  258.         return $this;
  259.     }
  260.     public function getRelativePath(): string
  261.     {
  262.         return $this->getFilePath().'/'.$this->getFileName();
  263.     }
  264.     public function getRelativePathEncrypted(bool $blur): string
  265.     {
  266.         $relativePath $this->getFilePath();
  267.         if ($blur) {
  268.             $relativePath .= '/'.$this->getBlurredFilename();
  269.         } else {
  270.             $relativePath .= '/'.$this->getNonBlurredFilename();
  271.         }
  272.         return $relativePath;
  273.     }
  274.     /**
  275.      * @Serializer\VirtualProperty()
  276.      *
  277.      * @Groups({"photocheck", "mediaindex", "actorlist", "groupdetails", "membermainphoto", "membermediaurl", "amateur_search"})
  278.      */
  279.     public function getUrl(?string $filter nullbool $withHost true): ?string
  280.     {
  281.         $host '';
  282.         if ($withHost) {
  283.             $host $_ENV['APP_HOST_ASSETS'];
  284.         }
  285.         if (null === $this->url) {
  286.             return $host.'/'.$this->getUrlForMemberMedia(false$filter);
  287.         }
  288.         if (null !== $filter) {
  289.             return str_replace('__filter__'$filter$this->url);
  290.         }
  291.         return $this->url;
  292.     }
  293.     /**
  294.      * @Serializer\VirtualProperty()
  295.      *
  296.      * @Groups({"photocheck", "mediaindex", "actorlist", "groupdetails", "membermainphoto", "membermediaurl", "amateur_search"})
  297.      */
  298.     public function getPixelatedUrl(?string $filter nullbool $withHost true): ?string
  299.     {
  300.         $host '';
  301.         if ($withHost) {
  302.             $host $_ENV['APP_HOST_ASSETS'];
  303.         }
  304.         if (null === $this->pixelatedUrl) {
  305.             return $host.'/'.$this->getUrlForMemberMedia(true$filter);
  306.         }
  307.         if (null !== $filter) {
  308.             return str_replace('__filter__'$filter$this->pixelatedUrl);
  309.         }
  310.         return $this->pixelatedUrl;
  311.     }
  312.     /**
  313.      * Possible outputs for video previews:
  314.      * https://assets.frivol.com/mediacache/media/preview_big/80/96/b0/744b8c9fb610b0ac772e52ee3b/1.pixelated.ed4e2f8a.png //UNHANDLED
  315.      * https://assets.frivol.com/mediacache/media/preview_small/80/96/b0/744b8c9fb610b0ac772e52ee3b/1.original.dd3897a5.png //UNHANDLED
  316.      *
  317.      * For imagesets:
  318.      * https://assets.frivol.com/mediacache/media/previmg_small/11/53/18/45a0576c887c01adeccd245201/1.original.f4d1ce44.png //UNHANDELD
  319.      *
  320.      * Admin "All photos":
  321.      * https://assets.frivol.com/mediacache/media/original/membermedia/5b/51/24/d144ecad96cd.jpg
  322.      *
  323.      * For profile images:
  324.      * https://assets.frivol.com/mediacache/media/500x375/membermedia/b8/14/5f/ad571c2a2f33.jpg
  325.      *
  326.      * For Groups
  327.      * https://assets.frivol.com/mediacache/media/100x100/membermedia/fb/e4/33/f6dfec7df32c.jpg
  328.      */
  329.     private function getUrlForMemberMedia(bool $blur, ?string $filter null): string
  330.     {
  331.         if ($blur) {
  332.             $encrypted $this->getBlurredFilename();
  333.         } else {
  334.             $encrypted $this->getNonBlurredFilename();
  335.         }
  336.         if (null === $filter) {
  337.             $search = ['__path__''__encryptedfilename__'];
  338.             $replace = [$this->getFilePath(), $encrypted];
  339.         } else {
  340.             $search = ['__filter__''__path__''__encryptedfilename__'];
  341.             $replace = [$filter$this->getFilePath(), $encrypted];
  342.         }
  343.         return str_replace(
  344.             $search,
  345.             $replace,
  346.             self::URL_MEDIACACHE_PATTERN,
  347.         );
  348.     }
  349.     public function getNonBlurredFilename(): string
  350.     {
  351.         $filename $this->getFileName();
  352.         $start substr($filename0, -4);
  353.         return $start.substr(md5($filename.MemberMediaService::SECRET_REGULAR), 04).'.jpg';
  354.     }
  355.     public function getBlurredFilename(): string
  356.     {
  357.         $filename $this->getFileName();
  358.         $start substr($filename0, -4);
  359.         return $start.substr(md5($filename.MemberMediaService::SECRET_BLUR), 04).'.jpg';
  360.     }
  361.     public function getUrlProperty(?string $filter null): string
  362.     {
  363.         return null === $filter ? (string) $this->url str_replace('__filter__'$filter, (string) $this->url);
  364.     }
  365.     public function getPixelatedUrlProperty(?string $filter null): string
  366.     {
  367.         return null === $filter ? (string) $this->pixelatedUrl str_replace('__filter__'$filter, (string) $this->pixelatedUrl);
  368.     }
  369. }