FileGrabber behavior: Fetch file from an uri with Upload's plugin #45

Closed
wants to merge 5 commits into
from

Conversation

Projects
None yet
2 participants
Contributor

hiryu85 commented Jan 31, 2012

Hi, I wrote a "FileGrabber" behavior for use this plugin with a remote file: download remote file on local disk, save on db meta datas, and thumbnails generation (if any).

@josegonzalez josegonzalez commented on the diff Feb 28, 2012

models/behaviors/file_grabber.php
@@ -0,0 +1,65 @@
+<?php
+if (!class_exists('UploadBehavior')) {
+ App::import('Behavior', 'Upload.Upload');
+}
+class FileGrabberBehavior extends UploadBehavior {
+
+ /**
+ * Download remote file into PHP's TMP dir
+ *
+ * @author Mirko 'hiryu' Chialastri
+ */
+ function _grab(&$model, $field, $uri) {
+ // debug('Grabbing file: '.$uri);
@josegonzalez

josegonzalez Feb 28, 2012

Owner

remove this debug statement

@josegonzalez josegonzalez commented on the diff Feb 28, 2012

models/behaviors/file_grabber.php
@@ -0,0 +1,65 @@
+<?php
+if (!class_exists('UploadBehavior')) {
+ App::import('Behavior', 'Upload.Upload');
+}
+class FileGrabberBehavior extends UploadBehavior {
+
+ /**
+ * Download remote file into PHP's TMP dir
+ *
+ * @author Mirko 'hiryu' Chialastri
+ */
+ function _grab(&$model, $field, $uri) {
+ // debug('Grabbing file: '.$uri);
+ App::import('HttpSocket');
@josegonzalez

josegonzalez Feb 28, 2012

Owner

Please follow proper formatting. Should be tabs, not spaces.

@josegonzalez josegonzalez commented on the diff Feb 28, 2012

models/behaviors/file_grabber.php
@@ -0,0 +1,65 @@
+<?php
+if (!class_exists('UploadBehavior')) {
+ App::import('Behavior', 'Upload.Upload');
+}
+class FileGrabberBehavior extends UploadBehavior {
+
+ /**
@josegonzalez

josegonzalez Feb 28, 2012

Owner

comments are flush to the margin, not indented.

@josegonzalez josegonzalez commented on the diff Feb 28, 2012

models/behaviors/file_grabber.php
+
+ /**
+ * Download remote file into PHP's TMP dir
+ *
+ * @author Mirko 'hiryu' Chialastri
+ */
+ function _grab(&$model, $field, $uri) {
+ // debug('Grabbing file: '.$uri);
+ App::import('HttpSocket');
+
+ $socket = new HttpSocket();
+ $socket->get($uri);
+ $headers = $socket->response['header'];
+ $file_name = basename($socket->request['uri']['path']);
+ $tmp_file = sys_get_temp_dir().'/'.$file_name;
+ $not_founded = $socket->response['status']['code'] != 200; // HttpSocket follow redirection?
@josegonzalez

josegonzalez Feb 28, 2012

Owner

Why are you continuing if the response code is not 200? Should return right at line 18 instead, no?

@hiryu85

hiryu85 Feb 28, 2012

Contributor

Yeah is true :)

@josegonzalez josegonzalez commented on the diff Feb 28, 2012

models/behaviors/file_grabber.php
+ $not_founded = $socket->response['status']['code'] != 200; // HttpSocket follow redirection?
+ // Populating data (like $_FILES)
+ $model->data[$model->alias][$field] = array(
+ $field => null,
+ 'name' => $file_name,
+ 'tmp_name' => $tmp_file,
+ 'error' => 1,
+ 'size' => $headers['Content-Length'],
+ 'type' => $headers['Content-Type']
+ );
+
+ if ($not_founded) return;
+ //debug('Write remote file into: '.$tmp_file);
+ $file = file_put_contents($tmp_file, $socket->response['body']);
+ if ($file === FALSE) {
+ debug('Writing error...');
@josegonzalez

josegonzalez Feb 28, 2012

Owner

No debug information should be output here. Maybe you can add a protected _log() method and turn off output by default

@josegonzalez josegonzalez commented on the diff Feb 28, 2012

models/behaviors/upload.php
@@ -492,7 +492,7 @@ function isWritable(&$model, $check) {
return true;
}
- return is_writable($this->settings[$model->alias][$field]['path']);
+ return is_writable(ROOT . DS . APP_DIR . DS. $this->settings[$model->alias][$field]['path']);
@josegonzalez

josegonzalez Feb 28, 2012

Owner

This is already fixed in master.

Owner

josegonzalez commented Jan 20, 2013

Merged into the master, 2.x branch. Thanks for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment