Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add s3 for uploads #675

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions www/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ METRIKA_HAWK_TOKEN = ''
# Ads tokens for carbon
CARBON_SERVE=
CARBON_PLACEMENT=

# AWS Keys
AWS_ACCESS_KEY_ID = ''
AWS_SECRET_ACCESS_KEY = ''
AWS_BUCKET = ''
AWS_REGION = 'us-east-1'
23 changes: 11 additions & 12 deletions www/application/classes/Controller/Base/Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

class Controller_Base_Ajax extends Controller_Base_preDispatch
{


/**
* Constants means action we did with transfered file
*/
* Constants means action we did with transfered file
*/
const TRANSPORT_ACTION_PROFILE_PHOTO = 1;
const TRANSPORT_ACTION_ARTICLE_COVER = 2;

Expand All @@ -15,6 +13,8 @@ class Controller_Base_Ajax extends Controller_Base_preDispatch
*/
public $UPLOAD_MAX_SIZE = '30M';

private $mediaDir = 'upload/users';


public function before()
{
Expand Down Expand Up @@ -47,36 +47,35 @@ public function action_file_uploader()
/** Target id */
$id = (int)Arr::get($_POST, 'id', 0);

/** Uploaded files */
$files = Arr::get($_FILES, 'files');
/** Uploaded file */
$file = Arr::get($_FILES, 'files');

/** Array will be passed to JS transport module */
$response = array('success' => 0);

/**
* Check for correct parametres
*/
$dataValidationError = $this->getTransportValidationError($action, $id, $files);
$dataValidationError = $this->getTransportValidationError($action, $id, $file);

if ($dataValidationError) {
$response['error_description'] = $dataValidationError;
} else {
switch ($action) {

case self::TRANSPORT_ACTION_PROFILE_PHOTO:

$file = $this->methods->saveImage($files, 'upload/users/');
$filename = Arr::get($file, 'name');

$filename = Model_Methods::saveMedia($file, $this->mediaDir);

if ($filename) {

/** Update user */
$this->user->photo = '/upload/users/b_' . $filename;
$this->user->photo = $filename;
$this->user->update();

/** Return success information. @uses client-side callback.saveProfilePhoto handler */
$response['success'] = 1;
$response['callback'] = 'codex.profile.uploadPhotoSuccess("/upload/users/b_'.$filename.'")';
$response['callback'] = 'codex.profile.uploadPhotoSuccess("'.$this->user->photo.'")';
} else {
$response['error_description'] = 'File wasn\'t saved';
}
Expand Down
77 changes: 17 additions & 60 deletions www/application/classes/Controller/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ class Controller_Transport extends Controller_Base_preDispatch
private $transportResponse = array(
'success' => 0
);

private $type = null;

private $file = null;

/**
* Where all image files will be stored
* Can be overridden, for example, for storing in a temporary dir
*/
private $mediaDir = 'upload/redactor_images/';
private $mediaDir = 'upload/editor';

private const MAX_MEDIA_SIZE = '30M';

Expand All @@ -34,26 +33,18 @@ public function action_file_uploader()
goto finish;
}

/**
* Download file from URL
*/
$this->file = $this->methods->getFile($url);
$imageData = $this->saveEditorMedia();

if ($imageData) {
$this->transportResponse['success'] = 1;
$this->transportResponse['file'] = array(
'url' => '/' . $this->mediaDir . 'o_' . Arr::get($imageData, 'name'),
'width' => Arr::get($imageData, 'width', 0),
'height' => Arr::get($imageData, 'height', 0)
);
}
goto finish;
} else {
/**
* Get file from $_FILES
*/
$this->file = Arr::get($_FILES, 'media');
}

/**
* Get file from $_FILES and detect its type
*/
$this->file = Arr::get($_FILES, 'media');

if ( ! $this->file || ! Upload::not_empty($this->file) || ! Upload::valid($this->file)) {
if ( ! $this->file || ! Upload::valid($this->file) ) {
$this->transportResponse['message'] = 'File is missing or damaged';
goto finish;
}
Expand All @@ -63,16 +54,14 @@ public function action_file_uploader()
goto finish;
}

$imageData = $this->saveEditorMedia($this->file);

if ($imageData) {
try {
$fileUrl = Model_Methods::saveMedia($this->file, $this->mediaDir);

$this->transportResponse['success'] = 1;
$this->transportResponse['file'] = array(
'url' => '/' . $this->mediaDir . Arr::get($imageData, 'name'),
'width' => Arr::get($imageData, 'width', 0),
'height' => Arr::get($imageData, 'height', 0)
$this->transportResponse['file'] = array(
'url' => $fileUrl
);
} else {
} catch (Exception $e) {
$this->transportResponse['message'] = 'Something went wrong while saving the file';
}

Expand All @@ -81,36 +70,4 @@ public function action_file_uploader()
$this->response->body(@json_encode($this->transportResponse));
}

private function saveEditorMedia()
{
$mediaType = $this->detectMediaType();

switch ($mediaType) {
case 'photo':
$imageData = $this->methods->saveImage($this->file, $this->mediaDir);
break;
case 'video':
$imageData = $this->methods->saveVideo($this->file, $this->mediaDir);
break;
default:
$this->transportResponse['message'] = 'Unsupported file type';
$imageData = false;
break;
}

return $imageData;
}

private function detectMediaType() {
$isPhoto = Upload::type($this->file, array('jpg', 'jpeg', 'png'));
$isVideo = Upload::type($this->file, array('mp4', 'mov', 'gif'));

if ($isPhoto) {
return 'photo';
} elseif ($isVideo) {
return 'video';
} else {
return NULL;
}
}
}
74 changes: 31 additions & 43 deletions www/application/classes/Model/Methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ public function num_decline($num, $nominative, $genitive_singular, $genitive_plu
}
}



public function save_cover($cover)
{
$new_name = bin2hex(openssl_random_pseudo_bytes(5));
Expand All @@ -80,8 +78,27 @@ public function save_cover($cover)
}
}

/**
* Save uploaded media to S3
*/
public static function saveMedia($file, $folder)
{
try {
$filename = Model_S3::upload($file, $folder);

// Delete the temporary file
unlink($file['tmp_name']);

return $filename;
} catch (Exception $e) {
return false;
}
}


/**
* DEPRECATED
*
* Save uploaded image
* @param resource $file - uploaded file
* @param string $path - path where image will be saved
Expand All @@ -97,47 +114,13 @@ public function saveImage($file, $path)
return false;
}

if (!is_dir($path)) {
mkdir($path);
}

if (isset($file['tmp_name'])) {
$filename = bin2hex(openssl_random_pseudo_bytes(16)) . '.jpg';
$image = Image::factory($file['tmp_name']);
$image->save($path . $filename);

$originalWidth = $image->width;
$originalHeight = $image->height;

foreach ($this->IMAGE_SIZES_CONFIG as $prefix => $sizes) {
$isSquare = !!$sizes[0];
$width = $sizes[1];
$height = !$isSquare ? $sizes[2] : $width;
$image->background('#fff');

// Вырезание квадрата
if ($isSquare) {
if ($image->width >= $image->height) {
$image->resize(null, $height, true);
} else {
$image->resize($width, null, true);
}
$image->crop($width, $height);
} else {
if ($image->width > $width || $image->height > $height) {
$image->resize($width, $height, true);
}
}

$image->save($path . $prefix . '_' . $filename);
}
$filename = Model_S3::upload($file, 'images');

// Delete the temporary file
unlink($file['tmp_name']);

return array(
'width' => $originalWidth,
'height' => $originalHeight,
'name' => $filename
);
}
Expand All @@ -146,6 +129,8 @@ public function saveImage($file, $path)
}

/**
* DEPRECATED
*
* Save uploaded video
* @param resource $file - uploaded file
* @param string $path - path where video will be saved
Expand All @@ -154,15 +139,15 @@ public function saveImage($file, $path)
*/
public function saveVideo($file, $path)
{
if (!is_dir($path)) {
mkdir($path);
if (!Upload::type($file, array('mp4', 'mov'))) {
return false;
}

if (isset($file['tmp_name'])) {
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
$filename = bin2hex(openssl_random_pseudo_bytes(16)) . '.' . $ext;

move_uploaded_file($file['tmp_name'], $path . $filename);
$filename = Model_S3::upload($file, 'images');

// Delete the temporary file
unlink($file['tmp_name']);

return array(
'name' => $filename
Expand All @@ -172,6 +157,9 @@ public function saveVideo($file, $path)
return false;
}

/**
* Download file by url
*/
public function getFile($url)
{
$tempName = tempnam('/tmp', 'tmp_files');
Expand Down
Loading