<?php
namespace App\Entity;
use App\Dictionary\MemberMediaType;
use App\Service\Media\MemberMediaService;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use JMS\Serializer\Annotation as Serializer;
use JMS\Serializer\Annotation\Exclude;
use JMS\Serializer\Annotation\Groups;
/**
* @ORM\Table("MemberMedia")
*
* @ORM\HasLifecycleCallbacks()
*
* @ORM\Entity(repositoryClass="App\Repository\MemberMediaRepository")
*/
class MemberMedia
{
// E.g. https://assets.frivol.com/mediacache/media/__filter__/membermedia/4a/d0/4a/9a3aedaa11a8.jpg
public const URL_MEDIACACHE_PATTERN = 'mediacache/media/__filter__/membermedia/__path__/__encryptedfilename__';
/**
* @Serializer\Type("integer")
*
* @Groups({"photocheck", "mediaindex", "actorlist", "groupdetails", "membermainphoto", "membermediaurl", "amateur_search"})
*
* @ORM\Id()
*
* @ORM\GeneratedValue()
*
* @ORM\Column(type="integer")
*/
private $id;
/**
* @Groups({"photocheck", "mediaindex", "actorlist", "groupdetails"})
*
* @ORM\Column(type="string", length=255)
*/
private $file_name;
/**
* @Groups({"photocheck", "mediaindex", "actorlist", "groupdetails"})
*
* @ORM\Column(type="string", length=255)
*/
private ?string $file_path = null;
/**
* @Groups({"photocheck", "mediaindex", "membermediaflags"})
*
* @ORM\Column(type="boolean", nullable=true)
*/
private ?bool $is_checked = false;
/**
* @Groups({"photocheck", "mediaindex", "membermediaflags"})
*
* @ORM\Column(type="boolean", nullable=true)
*/
private ?bool $is_hardcore = null;
/**
* @ORM\Column(type="datetime")
*/
private $uploaded_at;
/**
* @Exclude()
*
* @ORM\Column(type="string", length=32, nullable=true)
*/
private ?string $import_hash = null;
/**
* @Groups({"photocheck", "mediaindex"})
*
* @ManyToOne(targetEntity="App\Entity\Member",cascade={"persist"})
*
* @JoinColumn(name="member_id", referencedColumnName="id", onDelete="SET NULL")
*/
private ?Member $member = null;
/**
* @Groups({"photocheck", "mediaindex", "membermediaflags"})
*
* @ORM\Column(type="boolean", nullable=true)
*/
private ?bool $is_proof = false;
/**
* @Groups({"membermainphoto", "membermediaflags"})
*
* @Serializer\Accessor(getter="getIsMain")
*/
private ?bool $is_main = false;
/**
* @Groups({"membermediaflags"})
*
* @ORM\Column(type="smallint")
*/
private int $media_type = MemberMediaType::IMAGE;
/**
* @Groups({"mediaindex", "membermainphoto"})
*
* @ORM\Column(type="smallint")
*/
private $position = 0;
/**
* @Exclude()
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $url;
/**
* @Exclude()
*
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $pixelatedUrl;
/**
* @ORM\PrePersist()
*/
public function prePersist()
{
if (!$this->getUploadedAt()) {
$this->setUploadedAt(new \DateTime());
}
if (!$this->getFileName()) {
$this->setFileName($this->generateFileName());
}
if (!$this->getFilePath()) {
$this->setFilePath($this->generateFilePath());
}
}
public function getIsMain(): bool
{
$member = $this->getMember();
if (!$member) {
return false;
}
if ($mainPhoto = $member->getMainPhoto()) {
return $mainPhoto->getId() === $this->getId();
}
return false;
}
public function generateFilePath(): string
{
$path = md5(mt_rand(11111, 999999999).microtime(true));
return implode('/', [
substr($path, 0, 2),
substr($path, 2, 2),
substr($path, 4, 2),
]);
}
public function generateFileName(): string
{
$md5 = md5(mt_rand(11111, 999999999).microtime(true));
$extension = MemberMediaType::IMAGE == $this->getMediaType() ? '.jpg' : '.mp4';
return substr($md5, 0, 8).$extension;
}
public function getId()
{
return $this->id;
}
public function setUrl(?string $url): self
{
$this->url = $url;
return $this;
}
public function setPixelatedUrl(?string $url): self
{
$this->pixelatedUrl = $url;
return $this;
}
public function getPosition(): int
{
return $this->position;
}
/**
* @return $this
*/
public function setPosition(int $position): self
{
$this->position = $position;
return $this;
}
public function getFileName(): ?string
{
return $this->file_name;
}
public function setFileName(string $file_name): self
{
$this->file_name = $file_name;
return $this;
}
public function getFilePath(): ?string
{
return $this->file_path;
}
public function setFilePath(string $file_path): self
{
$this->file_path = $file_path;
return $this;
}
public function getIsChecked(): ?bool
{
return $this->is_checked;
}
public function setIsChecked(?bool $is_checked): self
{
$this->is_checked = $is_checked;
return $this;
}
public function getIsHardcore(): ?bool
{
return $this->is_hardcore;
}
public function setIsHardcore(?bool $is_hardcore): self
{
$this->is_hardcore = $is_hardcore;
return $this;
}
public function getUploadedAt(): ?\DateTimeInterface
{
return $this->uploaded_at;
}
public function setUploadedAt(\DateTimeInterface $uploaded_at): self
{
$this->uploaded_at = $uploaded_at;
return $this;
}
public function getImportHash(): ?string
{
return $this->import_hash;
}
public function setImportHash(?string $import_hash): self
{
$this->import_hash = $import_hash;
return $this;
}
public function getMember(): ?Member
{
return $this->member;
}
public function setMember(?Member $member): self
{
$this->member = $member;
return $this;
}
public function getIsProof(): ?bool
{
return $this->is_proof;
}
public function setIsProof(?bool $is_proof): self
{
$this->is_proof = $is_proof;
return $this;
}
public function getMediaType(): int
{
return $this->media_type;
}
public function setMediaType(int $media_type): self
{
$this->media_type = $media_type;
return $this;
}
public function getRelativePath(): string
{
return $this->getFilePath().'/'.$this->getFileName();
}
public function getRelativePathEncrypted(bool $blur): string
{
$relativePath = $this->getFilePath();
if ($blur) {
$relativePath .= '/'.$this->getBlurredFilename();
} else {
$relativePath .= '/'.$this->getNonBlurredFilename();
}
return $relativePath;
}
/**
* @Serializer\VirtualProperty()
*
* @Groups({"photocheck", "mediaindex", "actorlist", "groupdetails", "membermainphoto", "membermediaurl", "amateur_search"})
*/
public function getUrl(?string $filter = null, bool $withHost = true): ?string
{
$host = '';
if ($withHost) {
$host = $_ENV['APP_HOST_ASSETS'];
}
if (null === $this->url) {
return $host.'/'.$this->getUrlForMemberMedia(false, $filter);
}
if (null !== $filter) {
return str_replace('__filter__', $filter, $this->url);
}
return $this->url;
}
/**
* @Serializer\VirtualProperty()
*
* @Groups({"photocheck", "mediaindex", "actorlist", "groupdetails", "membermainphoto", "membermediaurl", "amateur_search"})
*/
public function getPixelatedUrl(?string $filter = null, bool $withHost = true): ?string
{
$host = '';
if ($withHost) {
$host = $_ENV['APP_HOST_ASSETS'];
}
if (null === $this->pixelatedUrl) {
return $host.'/'.$this->getUrlForMemberMedia(true, $filter);
}
if (null !== $filter) {
return str_replace('__filter__', $filter, $this->pixelatedUrl);
}
return $this->pixelatedUrl;
}
/**
* Possible outputs for video previews:
* https://assets.frivol.com/mediacache/media/preview_big/80/96/b0/744b8c9fb610b0ac772e52ee3b/1.pixelated.ed4e2f8a.png //UNHANDLED
* https://assets.frivol.com/mediacache/media/preview_small/80/96/b0/744b8c9fb610b0ac772e52ee3b/1.original.dd3897a5.png //UNHANDLED
*
* For imagesets:
* https://assets.frivol.com/mediacache/media/previmg_small/11/53/18/45a0576c887c01adeccd245201/1.original.f4d1ce44.png //UNHANDELD
*
* Admin "All photos":
* https://assets.frivol.com/mediacache/media/original/membermedia/5b/51/24/d144ecad96cd.jpg
*
* For profile images:
* https://assets.frivol.com/mediacache/media/500x375/membermedia/b8/14/5f/ad571c2a2f33.jpg
*
* For Groups
* https://assets.frivol.com/mediacache/media/100x100/membermedia/fb/e4/33/f6dfec7df32c.jpg
*/
private function getUrlForMemberMedia(bool $blur, ?string $filter = null): string
{
if ($blur) {
$encrypted = $this->getBlurredFilename();
} else {
$encrypted = $this->getNonBlurredFilename();
}
if (null === $filter) {
$search = ['__path__', '__encryptedfilename__'];
$replace = [$this->getFilePath(), $encrypted];
} else {
$search = ['__filter__', '__path__', '__encryptedfilename__'];
$replace = [$filter, $this->getFilePath(), $encrypted];
}
return str_replace(
$search,
$replace,
self::URL_MEDIACACHE_PATTERN,
);
}
public function getNonBlurredFilename(): string
{
$filename = $this->getFileName();
$start = substr($filename, 0, -4);
return $start.substr(md5($filename.MemberMediaService::SECRET_REGULAR), 0, 4).'.jpg';
}
public function getBlurredFilename(): string
{
$filename = $this->getFileName();
$start = substr($filename, 0, -4);
return $start.substr(md5($filename.MemberMediaService::SECRET_BLUR), 0, 4).'.jpg';
}
public function getUrlProperty(?string $filter = null): string
{
return null === $filter ? (string) $this->url : str_replace('__filter__', $filter, (string) $this->url);
}
public function getPixelatedUrlProperty(?string $filter = null): string
{
return null === $filter ? (string) $this->pixelatedUrl : str_replace('__filter__', $filter, (string) $this->pixelatedUrl);
}
}