Skip to content

Commit

Permalink
introduced element type file for direct uploading to S3 directories
Browse files Browse the repository at this point in the history
  • Loading branch information
phirschybar committed Jun 2, 2016
1 parent 06b1abf commit 88fac00
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
Binary file added public/img/file.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 28 additions & 15 deletions src/controllers/StationFileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public function upload()
$this->mime = $mime;
$extension = $file->getClientOriginalExtension();
$path = pathinfo($original_file_name);
$orig_name_wo_ext = $path['filename'];
$orig_name_wo_ext = preg_replace('/\W+/', '_', $path['filename']);
$new_file_name = $orig_name_wo_ext.'_'.date('Y-m-d-H-i-s').'.'.$extension;
$panel = new Panel;
$panel_name = $this->request->get('panel_name');
Expand All @@ -225,27 +225,40 @@ public function upload()
$app_config = StationConfig::app();
$success = FALSE;
$message = '';
$field_is_uploadable = $element['type'] == 'image' || (isset($element['embeddable']) && $element['embeddable']);
$field_is_uploadable = in_array($element['type'], ['image','file']) || (isset($element['embeddable']) && $element['embeddable']);

$this->request->file('uploaded_file')->move($this->tmp_dir, $new_file_name);

if ($field_is_uploadable){
if ($field_is_uploadable && $element['type'] != 'file'){

$allowed_image_extensions = ['png', 'gif', 'jpg', 'jpeg', 'PNG', 'GIF', 'JPG', 'JPEG'];
$bad_image = strpos($mime, 'image') === FALSE || !in_array($extension, $allowed_image_extensions);
$allowed_image_extensions = ['png', 'gif', 'jpg', 'jpeg'];
$bad_image = strpos($mime, 'image') === FALSE || !in_array(strtolower($extension), $allowed_image_extensions);

if ($bad_image) return Response::json(['success' => FALSE, 'reason' => 'not a proper image']);

$allow_upsize = isset($element['allow_upsize']) && $element['allow_upsize'];
$all_sizes = $panel->img_sizes_for($user_scope, $app_config);
$sizes_needed = isset($all_sizes[$element_name]) ? $all_sizes[$element_name] : $all_sizes['standard'];
$manipulations = $this->manipulate_sizes_and_send_each($new_file_name, $sizes_needed, $app_config, $allow_upsize);
$success = $manipulations['n_sent'] > 0;
$message = $manipulations['n_sent'].' manipulations made and sent to S3';
$allow_upsize = isset($element['allow_upsize']) && $element['allow_upsize'];
$all_sizes = $panel->img_sizes_for($user_scope, $app_config);
$sizes_needed = isset($all_sizes[$element_name]) ? $all_sizes[$element_name] : $all_sizes['standard'];
$manipulations = $this->manipulate_sizes_and_send_each($new_file_name, $sizes_needed, $app_config, $allow_upsize);
$success = $manipulations['n_sent'] > 0;
$message = $manipulations['n_sent'].' manipulations made and sent to S3';
$preview_uri = isset($manipulations['file_name']) ? 'http://'.$app_config['media_options']['AWS']['bucket'].'.s3.amazonaws.com/'.'station_thumbs_lg/'.$manipulations['file_name'] : FALSE;
$final_file_name = isset($manipulations['file_name']) ? $manipulations['file_name'] : FALSE;

} else { // file?
} else if ($field_is_uploadable && $element['type'] == 'file'){

// TODO: deal with non-images here. check for allowed types. then just move and send to S3.
$allowed_extensions = isset($element['allowed_types']) ? $element['allowed_types'] : ['zip', 'pdf', 'doc', 'xls', 'docx'];
$bad_file = !in_array(strtolower($extension), $allowed_extensions);
$target_directory = isset($element['directory']) ? $element['directory'] : '';

if ($bad_file) return Response::json(['success' => FALSE, 'reason' => 'not an allowed file type']);

$this->send_to_s3($new_file_name, $target_directory, $app_config, TRUE);

$success = TRUE;
$message = 'File sent to S3';
$preview_uri = '/public/packages/lifeboy/station/img/file.gif';
$final_file_name = $new_file_name;
}

$response = [
Expand All @@ -254,8 +267,8 @@ public function upload()
'message' => $message,
'insert_id' => isset($medium->id) ? $medium->id : FALSE,
'file_uri_stub' => 'http://'.$app_config['media_options']['AWS']['bucket'].'.s3.amazonaws.com/',
'file_uri' => isset($manipulations['file_name']) ? 'http://'.$app_config['media_options']['AWS']['bucket'].'.s3.amazonaws.com/'.'station_thumbs_lg/'.$manipulations['file_name'] : FALSE,
'file_name' => isset($manipulations['file_name']) ? $manipulations['file_name'] : FALSE
'file_uri' => $preview_uri,
'file_name' => $final_file_name,
];


Expand Down
4 changes: 2 additions & 2 deletions src/views/layouts/form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
$default_value = isset($element_info['default']) ? $element_info['default'] : null; // important to keep this as null
$default_value = isset($passed_model->$element_name) ? $passed_model->$element_name : $default_value;
$is_file_uploader = in_array($element_info['type'],['image','image_gallery']);
$is_file_uploader = in_array($element_info['type'],['image','image_gallery','file']);
$has_file_uploader = $is_file_uploader || (isset($element_info['embeddable']) && $element_info['embeddable']);
$is_embedder = $has_file_uploader && !$is_file_uploader;
$needs_media = $needs_media || $has_file_uploader;
Expand All @@ -146,7 +146,7 @@
@endif


{{-- Image/gallery handling --}}
{{-- Image/gallery/file handling --}}
@if ($has_file_uploader)

<?php
Expand Down
2 changes: 1 addition & 1 deletion src/views/partials/uploader.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</button>
<button type="button" class="btn btn-default file-remover" style="display:none;" id="remove_for_{{ $original_el_name }}">
<span class="fui-cross-inverted"></span>&nbsp;
Remove Image
Remove
</button>
</div>
<div class="btn-group">
Expand Down

0 comments on commit 88fac00

Please sign in to comment.