I'm using the laravel-rackspace-opencloud package to manage files on RackSpace's cloud files platform. Is it possible to use this library to create / delete file containers? I've not been able to find an example of this, and the README only seems to reference the management of files within containers that have already been created.
Please follow the steps to create / delete file containers
create rack-space file containers with laravel
$client = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, array(
'username' => 'XXXXXX','apiKey' => 'XXXXXX'));
try{
$ContainerName = 'todo'; // static for now
$objectStoreService = $client->objectStoreService(null, 'DFW');
$container = $objectStoreService->createContainer($ContainerName);
} catch (Guzzle\Http\Exception\ClientErrorResponseException $e) {
Log::info($e->getResponse());
}
Delete Containers
//1. conneciton
$client = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, array(
'username' => 'XXXXXX','apiKey' => 'XXXXXX'));
// 2. get region
$objectStoreService = $client->objectStoreService(null, 'DFW');
// 3. Get container.
$container = $objectStoreService->getContainer('{containerName}');
// 4. Delete container.
$container->delete();
Related
Laravel Version: 5.5
I have a problem when uploading a file. I have multiple files in the array, I am uploading these files two times in the same function for the first time I am checking pdf file version, and file dimensions this block of code works perfectly but in the second block of code I am again uploading these files for merging these files it gives me this error "The file "A4.pdf" was not uploaded due to an unknown error". When I remove the first block of code then the second block of code start working.I don't know where I did mistake, I have searched a lot but not found the answer.
This block of code checking pdf file version and dimensions.
$paper_size = array();
$del_files = array();
foreach ($files as $file) {
$filename = time().date('m-d-y').$file->getClientOriginalName();
$file->move(public_path().'/uploads/check_pdf_files/', $filename);
$version = $this->pdfVersion(public_path().'/uploads/check_pdf_files/'.$filename);
if($version > 1.5)
{
File::delete('public/uploads/check_pdf_files/'.$filename);
return Response::json(" Your PDF file version is greater than 1.4 which is not compatible with our system, Please make it lower version.", 400);
}
$get_paper_size = $this->get_pdf_dimensions('public/uploads/check_pdf_files/'.$filename);
$paper_size[] = $get_paper_size;
$del_files[] = $filename;
}
if(round($paper_size[0]['width']) != round($paper_size[1]['width']))
{
foreach ($del_files as $del)
{
File::delete('public/uploads/check_pdf_files/'.$del);
}
return Response::json(" Your Files dimensions is not matching please try with same dimensions.", 400);
}
This block of code using for merging the files.
$new_pdf_file = array();
foreach ($request->file as $merge_file)
{
$newFile_name = time().$merge_file->getClientOriginalName();
$merge_file->move('public/uploads/', $newFile_name);
$new_pdf_file[] = $newFile_name;
}
dd($new_pdf_file);
$pdf = new \LynX39\LaraPdfMerger\PdfManage;
foreach($new_pdf_file as $new)
{
$pdf->addPDF('public/uploads/dummy_uploads/'.$new, 'all');
}
$temp_name = time().$request->merge_name;
$pdf->merge('file',base_path(). '/public/uploads/' . Auth::user()->email . '/'.$temp_name.'.pdf', 'P');
foreach($new_pdf_file as $delete_new)
{
File::delete('public/uploads/dummy_uploads/'.$delete_new);
}
$user = DB::table('user_pdf_files')->insert([
'user_files' => $request->merge_name.'.pdf',
'filename' => $temp_name.'.pdf',
'type' => $request->type[0],
'user_id' => Auth::user()->id,
]);
Session::flash('success', 'Files Merged Successfully');
return Response::json('success', 200);`
You need the fully qualified path to where you want the files saved to. Anytime you are moving or copying a file, ensure that you are using public_path() with the relative path as a parameter. This function outputs the fully qualified path to the public folder. For example:
$merge_file->move(public_path('uploads'), $newFile_name);
This should be why the first code block is working vs. the second one. Not a very descriptive error, though!
For some reason I am not able to delete a file I upload with $request->file->store.
I tried \File::delete, \File::Delete, \Storage::Delete, and \Storage::delete. I want to avoid using unlink as that is not the laravel way.
May you please help me?
This is my code:
if ($user->avatar_path) {
// delete old one
\File::delete(app_path().$avatar_path);
}
$avatar_path = $request->file('avatar')->store('uploads/users/'.$user->id.'/avatar', 'public');
$user->update($request->except('avatar') + ['avatar_path' => $avatar_path]);
We see here that I check if there is an old avatar, and then I want to delete it. Then I upload the next one and update the database with the new path.
Finally solved this. I had to use Storage::disk('public')->delete($old_avatar_path);. Final code:
if ($request->has('avatar')) {
$old_avatar_path = $user->avatar_path;
$avatar_path = $request->file('avatar')->store('avatars', 'public');
$user->update($request->except('avatar') + ['avatar_path' => $avatar_path]);
if ($old_avatar_path) {
// delete old one
Storage::disk('public')->delete($old_avatar_path);
}
I'm getting a little confused here with this Storage VS Public paths in Laravel 5 > ... My understanding is one should use Storage:: instead of File:: for writing files to the Storage folder foresighting the use of cloud services like Amazon etc. So I am trying to put a jpg into a the storage/app folder using Intervention and this code:
public function apply(Request $request)
{
$user = Auth::user();
$path = '/users/'.$user->id;
if (!Storage::exists($path))
{
Storage::makeDirectory ($path, $mode = 0755, $recursive = false, $force = false);;
}
Image::make($request->file('image'))
->orientate()
->resize(600, null, function ($constraint) {
$constraint->aspectRatio();
})
->resizeCanvas(600, 600, 'center', false, 'ffffff')
->save($path.'/foo.jpg');
}
First of all I am not sure the !Storage::exists($path) will do anything as the API for storage tells it won't check for directories so how should I check if a Directory exists??
Second dd(is_writable($path)); return false, and indeed running that code results in
NotWritableException in Image.php line 143:
Can't write image data to path
error.
so how should this be done?
The "trick" that I used was manipulate the image directly in the temp path, and then save it in the storage folder using the Laravel storage method.
$tempFile = $request->file('image')->getRealPath();
Image::make($tempFile)
->resize(100, 100)
->save($tempFile); // Note that we are saving back to temporary path
// Now we can proceed and send the manipulated file to it's final destination
// in '/storage/app/public/uploads'
$path = $request->file('image')->storePublicly('public/uploads');
I have been using CakePHP 2.4.4 to build the interactive web part of my app and that is going very well. CakePHP is awesome.
I am now doing some supporting background processes. The Console and Shell seems to be the way to do it as it has access to the Models.
I have written the code and have it working but I am trying to write to the same log that I use for the Models. In the models I have an afterSave function to log all the database changes and I just used the $this->log("$model $logEntry", 'info'); to write to the log.
That doesn't work in the Shell but I thought the CakeLog::write('info', "$model $logEntry"); might work but it doesn't either.
Do I need to initialise the CakeLog to point to the correct log files?
<?php
App::uses('CakeTime', 'Utility');
App::uses('CakeLog', 'Utility');
class ProcessRequestShell extends AppShell {
//Need to access the request and monitor tables
public $uses = array('Request');
private function updateRequest($data){
$model = 'Request';
$result = $this->Request->save($data);
$logEntry = "UPDATE ProcessRequestShell ";
foreach ($data[$model] AS $k => $v){$logEntry .= "$k='$v' ";}
if ($result){
//$this->log("$model $logEntry", 'info');
CakeLog::write('info', "$model $logEntry");
} else {
//$this->log("$model FAILED $logEntry", 'error');
CakeLog::write('error', "$model FAILED $logEntry");
}
return($result);
}
public function main() {
$options = array('conditions' => array('state' => 0, 'next_state' => 1));
$this->Request->recursive = 0;
$requests = $this->Request->find('all', $options);
//See if the apply_changes_on date/time is past
foreach ($requests AS $request){
$this->out("Updating request ".$request['Request']['id'], 1, Shell::NORMAL);
//Update the next_state to "ready"
$request['Request']['state'] = 1;
$request['Request']['next_state'] = 2;
$this->updateRequest($request);
}
}
}
?>
Did you set up a default listener/scope for those log types?
If not, they will not get logged.
// Setup a 'default' cache configuration for use in the application.
Cache::config('default', array('engine' => 'File'));
In your bootstrap.php for example
See http://book.cakephp.org/2.0/en/appendices/2-2-migration-guide.html#log
You need to setup default log stream writing to file, eventually, in app/Config/bootstrap.php.
CakeLog does not auto-configure itself anymore. As a result log files
will not be auto-created anymore if no stream is listening. Make sure
you got at least one default stream set up, if you want to listen to
all types and levels. Usually, you can just set the core FileLog class
to output into app/tmp/logs/:
CakeLog::config('default', array(
'engine' => 'File'
));
See Logging → Writing to logs section of the CookBook 2.x
My version of YII: 1.1.12... scratch that, I upgraded to version 1.1.13, and still doesn't work.
I tried this:
Yii::app()->cache->set('someKey', $auctions);
$data = Yii::app()->cache->get('someKey');
print_r($data);
And I see the data that I stored! However, if I try this:
Yii::app()->cache->set('someKey', $auctions, 10);
$data = Yii::app()->cache->get('someKey');
print_r( $data );
I see nothing? Why is YII ignoring my time interval? What am I missing?
** EDIT **
My caching is defined in config as:
'cache'=>array(
'class'=>'system.caching.CMemCache',
'useMemcached'=>false,
'servers'=>array(
array( 'host'=>'127.0.0.1', 'port'=> 11211, 'weight'=>60 ),
//array('host'=>'server2', 'port'=>11211, 'weight'=>40),
),
),
I know the Memcache is working, because I've tested it with this example outside of the YII framework:
$memcache = new Memcache;
$memcache->connect("localhost",11211);
$tmp_object = new stdClass;
$tmp_object->str_attr = "test";
$memcache->set("mysupertest",$tmp_object,false,5);
var_dump($memcache->get("mysupertest"));
This works and the item is cached for 5 seconds...
It seems like it's a bug in CMemCache.php. There is this function:
protected function setValue($key,$value,$expire)
{
if($expire>0)
$expire+=time();
else
$expire=0;
return $this->useMemcached ? $this->_cache->set($key,$value,$expire) : $this->_cache->set($key,$value,0,$expire);
}
MemCache does not want the time to be added, so my quick fix is:
protected function setValue($key,$value,$expire)
{
return $this->useMemcached ? $this->_cache->set($key,$value,$expire) : $this->_cache->set($key,$value,0,$expire);
}
Well, make sure $auctions are well defined.
Yii::app()->cache->set('someKey', array('someValue'), 120); // 120 means 2 minutes
print_r(Yii::app()->cache->get('someKey')); // you should see the array with the single value, I do see it when I try to run it
Make sure the config is OK and you are not using CDummyCache. Mine looks like this:
'components' => array(
...
// Add a cache component to store data
// For demo, we are using the CFileCache, you can use any
// type your server is configured for. This is the simplest as it
// requires no configuration or setup on the server.
'cache' => array (
'class' => 'system.caching.CFileCache',
),
...
),