public function __construct(string $uid = null)
 {
     if (!$uid) {
         $uid = GenerateRandomString::gen(self::UID_LENGTH);
     }
     $this->uid = $uid;
 }
 private function createEmptyImages(ImageStrategy $strategy) : ImageCollection
 {
     $collection = new ImageCollection();
     array_map(function (int $size) use($collection, $strategy) {
         $dir = $this->touchDir($strategy->getFilesystem(), $strategy->getEntityId(), $collection->getUID(), $size);
         $file = sprintf('%s.png', GenerateRandomString::gen(self::GENERATE_FILENAME_LENGTH));
         $strategy->getFilesystem()->write("{$dir}/{$file}", '');
         $collection->attachImage((string) $size, new ImageCollectionItem("{$dir}/{$file}", sprintf("%s/%s/%s/%s", $strategy->getPublicPath(), $strategy->getEntityId(), $size, $file)));
     }, $strategy->getSizes());
     return $collection;
 }
 public function backdropUpload(BackdropEntityAware $entity, BackdropUploadStrategy $strategy, string $textColor, string $tmpFile) : UploadedBackdrop
 {
     $this->validateFile($strategy, $tmpFile);
     $uid = join('/', str_split($entity->getSID(), 2));
     $fileName = GenerateRandomString::gen(8) . '_' . FileNameFilter::filter(basename($tmpFile));
     $storagePath = sprintf('%s/%s/%s', $strategy->getStoragePath(), $uid, $fileName);
     $publicPath = sprintf('%s/%s/%s', $strategy->getPublicPath(), $uid, $fileName);
     $strategy->getFileSystem()->write($fileName, $tmpFile);
     $backdrop = new UploadedBackdrop($storagePath, $publicPath, $textColor);
     $entity->setBackdrop($backdrop);
     return $backdrop;
 }
Exemple #4
0
 public function createOAuth2Account(RegistrationRequest $registrationRequest) : Account
 {
     $email = $registrationRequest->getEmail();
     $provider = $registrationRequest->getProvider();
     $providerAccountId = $registrationRequest->getProviderAccountId();
     $account = $this->createAccount($email, GenerateRandomString::gen(32));
     $oauthAccount = new OAuthAccount($account);
     $oauthAccount->setProvider($provider);
     $oauthAccount->setProviderAccountId($providerAccountId);
     $this->accountRepository->createOAuth2Account($oauthAccount);
     return $account;
 }
Exemple #5
0
 public function uploadImagePreview(int $themeId, string $path) : string
 {
     $theme = $this->getThemeById($themeId);
     $dir = $theme->getId();
     $name = sprintf('%s.png', GenerateRandomString::gen(self::GENERATE_FILENAME_LENGTH));
     $newPath = sprintf('%s/%s', $dir, $name);
     if ($this->fileSystem->has($dir)) {
         $this->fileSystem->deleteDir($dir);
     }
     $this->fileSystem->write($newPath, file_get_contents($path));
     $theme->setPreview($newPath);
     $this->themeRepository->saveTheme($theme);
     return $theme->getPreview();
 }
 public function uploadAttachment(string $tmpFile, string $desiredFileName) : Attachment
 {
     $desiredFileName = FileNameFilter::filter($desiredFileName);
     $attachmentType = $this->factoryFileAttachmentType($tmpFile);
     if ($attachmentType instanceof FileAttachmentType) {
         $this->validateFileSize($tmpFile, $attachmentType);
     }
     $subDirectory = join('/', str_split(GenerateRandomString::gen(12), 2));
     $storagePath = $subDirectory . '/' . $desiredFileName;
     $publicPath = sprintf('%s/%s/%s', $this->wwwDir, $subDirectory, $desiredFileName);
     $finfo = new \finfo(FILEINFO_MIME);
     $content = file_get_contents($tmpFile);
     $contentType = $finfo->buffer($content);
     if ($this->fileSystem->write($storagePath, $content) === false) {
         throw new \Exception('Failed to copy uploaded file');
     }
     $result = new Result($publicPath, $content, $contentType);
     $source = new LocalSource($publicPath, $storagePath);
     return $this->linkAttachment($publicPath, $subDirectory, $desiredFileName, $result, $source);
 }
 public function generateImagesFromSource(ImageStrategy $strategy, ImageLayer $source) : ImageCollection
 {
     $collection = new ImageCollection();
     array_map(function (int $size) use($source, $collection, $strategy) {
         $ratio = array_filter(explode(':', $strategy->getRatio()), function ($input) {
             return is_numeric($input);
         });
         if (count($ratio) !== 2) {
             throw new InvalidRatioException(sprintf('Invalid ratio `%s`', $strategy->getRatio()));
         }
         $width = $size;
         $height = $size / (int) $ratio[0] * (int) $ratio[1];
         $image = clone $source;
         $image->resize($width, $height);
         $dir = $this->touchDir($strategy->getFilesystem(), $strategy->getEntityId(), $collection->getUID(), $size);
         $file = sprintf('%s.png', GenerateRandomString::gen(self::GENERATE_FILENAME_LENGTH));
         $strategy->getFilesystem()->write("{$dir}/{$file}", $image->encode('png'));
         $collection->attachImage((string) $size, new Image("{$dir}/{$file}", sprintf("%s/%s/%s/%s/%s", $strategy->getPublicPath(), $strategy->getEntityId(), $collection->getUID(), $size, $file)));
     }, $strategy->getSizes());
     return $collection;
 }
Exemple #8
0
 public function run(ServerRequestInterface $request, ResponseBuilder $responseBuilder) : ResponseInterface
 {
     try {
         /** @var UploadedFile $file */
         $file = $request->getUploadedFiles()["file"];
         $filename = $file->getClientFilename();
         if (!strlen($filename)) {
             $filename = GenerateRandomString::gen(self::AUTO_GENERATE_FILE_NAME_LENGTH);
         }
         if ($file->getError() !== 0) {
             throw new FileNotUploadedException('Failed to upload file');
         }
         $entity = $this->attachmentService->uploadAttachment($file->getStream()->getMetadata('uri'), $filename);
         $responseBuilder->setStatusSuccess()->setJson(['entity' => $entity->toJSON()]);
     } catch (FileTooBigException $e) {
         $responseBuilder->setStatus(409)->setError($e);
     } catch (FileTooSmallException $e) {
         $responseBuilder->setStatus(409)->setError($e);
     } catch (AttachmentFactoryException $e) {
         $responseBuilder->setStatusBadRequest()->setError($e);
     }
     return $responseBuilder->build();
 }
Exemple #9
0
 public function regenerateAPIKey() : string
 {
     $this->apiKey = GenerateRandomString::gen(12);
     return $this->apiKey;
 }
Exemple #10
0
 public function regenerateSID() : string
 {
     $this->sid = GenerateRandomString::gen(SIDEntity::SID_LENGTH);
     return $this->sid;
 }
Exemple #11
0
 private function createAccountFromJSON(array $json) : Account
 {
     if ($this->accountService->hasAccountWithEmail($json['email'])) {
         return $this->accountService->getByEmail($json['email']);
     } else {
         $account = $this->accountService->createAccount($json['email'], GenerateRandomString::gen(12));
         $profile = $account->getCurrentProfile();
         $profile->setGender(Gender::createFromIntCode((int) $json['gender']));
         $parameters = new EditPersonalParameters('fl', false, $json['username'] ?? '', $json['surname'] ?? '', $json['patronymic'] ?? '', $json['nickname'] ?? '');
         $this->profileService->updatePersonalData($profile->getId(), $parameters);
         $this->profileService->setInterestingInThemes($profile->getId(), $json['interests']);
         if ($json['birthday']) {
             $this->profileService->setBirthday($profile->getId(), \DateTime::createFromFormat('Y-m-d', $json['birthday']));
         }
         $avatarPath = sprintf("%s/%s", self::AVATAR_DIR, $json['avatar']);
         if (file_exists($avatarPath)) {
             list($width, $height) = getimagesize($avatarPath);
             if (!is_null($width) && !is_null($height)) {
                 $size = min($width, $height);
                 $parameters = new UploadImageParameters($avatarPath, new Point(0, 0), new Point($size, $size));
                 $this->profileService->uploadImage($profile->getId(), $parameters);
             }
         }
         return $account;
     }
 }