Cakephp force download makes site useless - cakephp

I am facing a rare problem with cakephp force download code. Below is my code:-
public function download($cid = null,$id = null) {
$this->viewClass= 'Media';
$Video = $this->Video->findById($id);
$ext = explode('.',$Video['Video']['name']);
$params = array(
'id' => $Video['Video']['name'],
'name' => $Video['Video']['title'],
'download' => true,
'extension' => $ext[1],
'path' => APP . 'webroot/movies/clips_mov'.DS
);
$this->set($params);
}
This is the code I am using since ages and almost all the tutorials on the web are point to this code only. Below is the code of download link code onclick of which executes this code and file starts downloading:-
<a href="<?php echo Router::url('/videos/download/1/'.$video['Download']
['video_id'], true);?>" >Download</a>
Now the problem starts. When I click the download link the file downloading starts and goes on well. But during the time file is downloading the site becomes useless. No matter on whatever link or button i click on the site the page keeps on loading but never loads. The files are bit large most of them are from 200MB to 1GB . The site becomes again useful and starts working fine when the file download is finish or if I cancel the downloading. But during the download the site becomes useless and we cannot browse it till then.
Please help me. Thanks in advance.

Related

CakePHP 1.3 cleares all cached pages after adding new post

I am using CakePHP 1.3 and trying to enable cache for view pages, cache system works fine and caches all pages. But when we add a new post (insert new record to database) or edit an old one (update a record of the table) CakePHP deletes all cached pages, not just the edited page!
app/config/core.php :
Cache::config('default', array('engine' => 'File','duration' => 8640000));
app/controllers/articles_controller.php :
var $helpers = array('Cache');
var $cacheAction = array(
'view' => array('duration' => 8640000),
'latest' => array('duration' => 8640000),
);
How can I tell Cake to delete just the cached version of changed page and not all cached pages?
This it actually pretty hard, so I can't just give you a piece of code to solve this. You need to edit the actual cake files in the lib folder that manage caching. Note: this is super not recommended by the cake people. However the lib/Cake/Cache/Engine/FileEngine.php is the file that has the operations of the file engine. You seem interested in the delete function:
/**
* Delete a key from the cache
*
* #param string $key Identifier for the data
* #return boolean True if the value was successfully deleted, false if it didn't exist or couldn't be removed
*/
public function delete($key) {
if ($this->_setKey($key) === false || !$this->_init) {
return false;
}
$path = $this->_File->getRealPath();
$this->_File = null;
//#codingStandardsIgnoreStart
return #unlink($path);
//#codingStandardsIgnoreEnd
}
Also, instead of editing the core cake files you could add your own file engine and use parted of the cake engine by moving the code and just extending the code there (that's totally cool in open source).
Its also possible that by reading the code used to implement the file caching engine you will find your actual solution. Good Luck.

Redirect to controller action if file not found

I'm trying to configure the rewrite rules for lighttpd so that a specific cakephp controller-action is executed if a file is not found. The controller-action will generate the data (png file), save it for future use, and then serve it to the client. The next time someone tries to access the file, it will be served by lighttpd directly without executing any php. In other words, the png files are cached so there is no need to recreate them.
From what I can tell, url.rewrite-if-not-file is ignored if rewrite-once has a match. Thus, the following can serve up my cached files but not my uncached files.
url.rewrite-if-not-file = (
"^/scan/(.+)\.png" => "/mycontroller/scan/$1"
)
url.rewrite-once = (
"^/(css|files|img|js)/(.*)" => "/$1/$2",
"^/favicon.ico" => "/favicon.ico",
"^/scan/(.+\.png)" => "/scan/$1",
"^([^\?]*)(\?(.+))?$" => "/index.php?url=$1&$3",
)
The only solution I can think of now is to delete the 3rd rule and modify ^([^\?]*)(\?(.+))?$ so it ignores urls starting with \scan\.
Any other suggestions?
i'd do something like this in a controller:
if (file_exists($file_name)) {
$this->redirect($file_name);
} else {
$this->redirect(array('action' => 'some_action', 'controller' => 'some_controller'));
}

CakePHP: Reporting Failed Downloads with the Media View

I'm using CakePHP's Media view to force file downloads. My code is pretty much exactly like the example provided in the cookbook, which I'll paste here for your convenience:
<?php
class ExampleController extends AppController {
public function download () {
$this->viewClass = 'Media';
// Download app/outside_webroot_dir/example.zip
$params = array(
'id' => 'example.zip',
'name' => 'example',
'download' => true,
'extension' => 'zip',
'path' => APP . 'outside_webroot_dir' . DS
);
$this->set($params);
}
}
In the database, I have a field that keeps track of how many times the file was downloaded. I'm looking for a way to make sure that this number is as accurate as possible, so if a user's download gets cancelled or times out, the number does not increment. Is there some way for CakePHP's Media view to report that the download was, indeed, successful?
Detecting when a file has finished downloading is no easy task. This is something that would be done on the client side with javascript, but browsers do not give you any hooks for that.
There is a pretty clever solution here (setting a cookie and then looking for it with javascript), but it only tells you when the download has started.

Integrating a file browser into ckeditor & CakePHP

recently found an article in http://hashtwo.com/blog/integrating-a-file-browser-into-ckeditor-cakephp to integrate a file browser into ckeditor (file manager of core five labs )
I followed the steps but i get an error when I click the browse server button,
Missing Controller:
Error: CkeditorController could not be found.
I had the same error at some point during my setup, but I don't seem to recall what was it and how I fixed it. Let me know if my next solution does not fix your problem and I will investigate a little more:
First I followed the following tutorial which worked well, but missed the Authentication part: http://labs.corefive.com/projects/filemanager/
Then I used the tutorial you referenced, except for the last section of code provided. Instead I added the following code directly to my view page: admin_add.ctp
<?php echo $form->textarea('body', array(/*'class'=>'ckeditor', */'id' => 'body', 'class' => 'body')) ?>
<?php
include_once 'ckeditor/ckeditor.php' ;
//require_once 'ckfinder/ckfinder.php' ;
//$initialValue = 'Default value goes here' ;
$ckeditor = new CKEditor() ;
$ckeditor->basePath = '/ckeditor/' ;
$ckeditor->config['filebrowserBrowseUrl'] = '/ckeditor/filemanager/index.html';
$ckeditor->config['filebrowserUploadUrl'] = '/ckeditor/filemanager/connectors/php/filemanager.php';
$ckeditor->config['filebrowserImageBrowseUrl'] = '/ckeditor/filemanager/index.html?type=Images';
$ckeditor->config['filebrowserImageUploadUrl'] = '/ckeditor/filemanager/connectors/php/filemanager.php?command=QuickUpload&type;=Images';
$ckeditor->config['filebrowserWindowWidth'] = '800';
//CKFinder::SetupCKEditor( $ckeditor,'/ckfinder/' ) ;
// This will replace CakePHP textarea listed above.
$ckeditor->replace('body');
?>
Also, for the Auth Section of his tutorial I changed it to match my requirements:
function auth() {
session_name("CAKEPHP");
session_start();
if(isset($_SESSION['Auth']['User']) )
{
//Since CKEditor for me is only used in the admin section
//I make sure only admins can access it, group_id=1
if($_SESSION['Auth']['User']['group_id'] == 1)
{
return true;
}
}
return false;
}
If it tries to find a controller this means it can not access the editors file or folder and instead the dispatcher of CakePHP gets the request and tries to load a fckeditor controller.
This means you'll need to place the editor in the right directory (for example app/webroot/fckedtor/) or use the correct url to access it.

Batch creative image update with ADS Api?

I try to update ads group with batch request. Everything is going fine if not add image_file param but when add it return an error.
My ad_specs;
Array
(
[method] => post
[relative_url] => 6003452316521?name=ReklamGrubu915&max_bid=50&bid_type=1&targeting={"gender":["1","2"],"age_min":"30","age_max":"40","countries":["TR"],"keywords":["#Nike, Inc."],"cities":[{"name":"Istanbul"},{"name":"Ankara"},{"name":"Izmir"}]}&creative={"title":"Ev Yemekleri","body":"Evde harika lezzetler!","link_url":"http://www.xxxxx.com/dealer.php?kat=334","image_file":"img17.jpg"}
)
And also add image to batch_params
$batch_params['img17.jpg'] = '/var/www/images/img17.jpg'
i send it but its return this error;
Error 1487242 - Image Resize Failed - the getimagesize function returned false
If i remove image_file param every update is going fine so batch request true. Image file exist in right directory. i try to change image_file params to 'attached_files'. Then there is no errors looking everything went right, return data is true but i see facebook ads manage page there is no image change?
Any ideas ? May its a bug please help ? I am using PHP SDK.
When you specify the file name of image in your creative, you have to add the image file as multi-part MIME POST. You are currently giving the image path. ($batch_params['img17.jpg'] = '/var/www/images/img17.jpg'). Effectively you need to upload the image to facebook with request.
Alternatively, you can first create a image in library using Fb Ad Image Api and then use the image_hash in creative.
$image = new AdImage(null, 'act_<ACCOUNT_ID>');
$image->{AdImageFields::FILENAME} = '<IMAGE_PATH>';
$image->create();
echo 'Image Hash: '.$image->{AdImageFields::HASH}.PHP_EOL;
and then
$creative = new AdCreative(null, 'act_<ACCOUNT_ID>');
$creative->setData(array(
AdCreativeFields::TITLE => 'My Test Creative',
AdCreativeFields::BODY => 'My Test Ad Creative Body',
AdCreativeFields::OBJECT_URL => 'https://www.facebook.com/facebook',
AdCreativeFields::IMAGE_HASH => '<IMAGE_HASH>',
));

Resources