Using Laravel's File::makeDirectory to create a folder in the public directory - directory

I have the following code in my controller - I am trying to create a directory structure to save files into:
use File;
...
$path = public_path().'/shared/uploads/images/brands/400';
if (!File::exists($path)) {
File::makeDirectory($path, $mode = 0755, true, true);
}
When I run the controller I get the following error:
Intervention\Image\Exception\NotWritableException: Can't write image data to path (/home/tony/Git/website/public/shared/uploads/images/brands/800/17.png) in file /home/tony/Git/website/vendor/intervention/image/src/Intervention/Image/Image.php on line 150
Looking in my public directory I can see no directory structure has been created.
Looking at other examples of this code doing similar/same thing I can't see any difference with my code except that I am creating the directories in the public folder.
As always, any help appreciated.

This was a permission issue.
On my development machine I simply changed the owner of /shared to www-data:www-data:
sudo chown www-data:www-data ./shared/
Once I'd done that the controller worked fine, creating the rest of the directory structure.

Related

Hosting Symfony 4 app with EasyDeployBundle on server without /usr/local/bin/composer

For a Symfony 4 app I have chosen a Web Cloud plan from the hosting provider OVH.
For the deployment I have decided to use the EasyDeployBundle which looks very promising. This is my config file:
<?php
use EasyCorp\Bundle\EasyDeployBundle\Deployer\DefaultDeployer;
return new class extends DefaultDeployer
{
public function configure()
{
return $this->getConfigBuilder()
->server('ovh')
->deployDir('directory/path/at/server')
->repositoryUrl('git#github.com:foo/bar.git')
->repositoryBranch('master')
;
}
}
I have .ssh/config file with the following entry:
Host ovh
Hostname sshcloud.foobar.hosting.ovh.net
Port 12345
User foobar
Note: all values are dummies, just for illustrational purposes.
When I run:
php bin/console deploy --dry-run -v
everything goes fine, but when I actually try to deploy I get the following error:
The command "ssh ovh 'which /usr/local/bin/composer'" failed.
The problem is that I have no write-access to the directory /usr/local/bin/ on the server. The composer.phar is in my home directory and I can't move it to the provisioned destination.
Is there any possibility to tell EasyDeployBundle to look for composer in another directory?
I should really read the manuals, in particular when I'm linking them in my question.
There is a method remoteComposerBinaryPath that accepts custom path to composer. I have amended the method configure like this:
public function configure()
{
return $this->getConfigBuilder()
->server('ovh')
->deployDir('directory/path/at/server')
->repositoryUrl('git#github.com:foo/bar.git')
->repositoryBranch('master')
->remoteComposerBinaryPath('composer.phar')
;
}
On the server I created .bashrc in my home folder and added the line:
export PATH=$PATH:/home/foobar
and now the deployment is passing this hurdle.
I have now another problem, but at least this one is solved and maybe the answer can help other people too.

symfony2 relative directory routes from command

I'm facing a little trouble with the relative directories from command line.
I have created a command (php app/console generate:command). With this command I need to delete some files in the "web/myFolder" directory of the project.
protected function execute(InputInterface $input, OutputInterface $output)
{
//$directorio = 'myFolder'; /*Do not work*/
$directorio = '\XAMPP\htdocs\MiProject\web\myFolder';
$files = scandir($directorio);
foreach($files as $f){ /*do something*/ }
}
From the command, I have to write the complete directory from D:/ so I don't have a relative directory from the symfony project, like I do in any Controller. Is there any solution to use a relative directory from the project folder?
With a non relative directory, I should replace this directory when put the project on a production server. So I should change it many time between test and production environment.
Lot of thanks.
You could use the predefined parameters like kernel.root_dir that usually point to the dir where the AppKernel file is locate (<project_root>/app).
So simply access to the params located in the container, as example
// From controller
$this->getParameters('kernel.root_dir');
or more generally:
// from service/command
$this->container->getParameters('kernel.root_dir');
So in your case should be:
$directorio = $this->getContainer()->getParameter('kernel.root_dir').'/../‌​web/myFolder';
some info here in this article.
Hope this help

Update solr config files in solrcloud

Hi fellow SOLR developers,
https://cwiki.apache.org/confluence/display/solr/Using+ZooKeeper+to+Manage+Configuration+Files This link says the below
To update or change your SolrCloud configuration files:
1. Download the latest configuration files from ZooKeeper, using the
source control checkout process.
2. Make your changes. Commit your changed file to source control.
3. Push the changes back to ZooKeeper.
4. Reload the collection so that the changes will be in effect.
But I was wondering if there are some examples or articles somewhere which can help me do this. For instance, how would one download the latest config files from zookeeper? Or should I know Zoopkeeper in-and-out to do this?
Version 5.3.1
I updated the synonym file in the "C:\12345\solrcloud\solr-5.3.1\example\example-DIH\solr\db\conf" folder and I'm using the below command
zkcli -zkhost localhost:2181,localhost:2182,localhost:2183 -cmd upconfig -confname db_config -confdir "C:\12345\solrcloud\solr-5.3.1\example\example-DIH\solr\db\conf"
But the file doesn't seem to change nor the query seem to work. I reloaded the core which did not help too. I even tried restarting my solrcloud.
My Brother, you are using upconfig command....
Same way there is a command called downconfig with same parameters.
So your order of execution will be :
Downconfig
Change the config
Upconfig
Downconfig command :
bin/solr zk downconfig -z 111.222.333.444:2181 -n mynewconfig -d /path/to/configset
Upconfig command :
bin/solr zk upconfig -z 111.222.333.444:2181 -n mynewconfig -d /path/to/configset
I think this short code can help to download the configs of solrcloud from zookeeper.
public void exportSynonyms(...) {
try {
final CloudSolrClient cloudSolrClient = ... //Retrieve the Solr client
final String configName = cloudSolrClient.getDefaultCollection();
final Path configPath = ... //Get or create temporary local file path
cloudSolrClient.downloadConfig(configName, configPath);
// Do something
cloudSolrClient.uploadConfig(configPath, configName);
// Reload the collection
} catch (RestClientException | SolrServiceException | IOException var11){
this.handleException(var11);
}
}
And I met the same problem and asked it on
https://cwiki.apache.org/confluence/display/solr/Using+ZooKeeper+to+Manage+Configuration+Files?focusedCommentId=65147407#comment-65147407
Hope it can help you.
After you update the file in Zookeeper, you need to tell solr to reload the collection, this tells solr to get the new configuration from Zookeeper.
See the Reload Solr Collection REST call:
https://cwiki.apache.org/confluence/display/solr/Collections+API#CollectionsAPI-api2

Setting Jetty resourcebase to static file embedded in the same jar file

I am trying to access static resource (eg. first.html) packed inside the same .jar file (testJetty.jar), which also has a class which starts the jetty (v.8) server (MainTest.java). I am unable to set the resource base correctly.
The structure of my jar file (testJetty.jar):
testJetty.jar
first.html
MainTest.java
==
Works fine on local machine, but when I wrap it in jar file and then run it, it doesn't work, giving "404: File not found" error.
I tried to set the resourcebase with the following values, all of which failed:
a) Tried setting it to .
resource_handler.setResourceBase("."); // Results in directory containing the jar file, D:\Work\eclipseworkspace\testJettyResult
b) Tried getting it from getResource
ClassLoader loader = this.getClass().getClassLoader();
File indexLoc = new File(loader.getResource("first.html").getFile());
String htmlLoc = indexLoc.getAbsolutePath();
resource_handler.setResourceBase(htmloc); // Results in D:\Work\eclipseworkspace\testJettyResult\file:\D:\Work\eclipseworkspace\testJettyResult\testJetty1.jar!\first.html
c) Tried getting the webdir
String webDir = this.getClass().getProtectionDomain()
.getCodeSource().getLocation().toExternalForm();
resource_handler.setResourceBase(webdir); // Results in D:/Work/eclipseworkspace/testJettyResult/testJetty1.jar
None of these 3 approaches worked.
Any help or alternative would be appreciated
Thanks
abbas
The solutions provided in this thread work but I think some clarity to the solution could be useful.
If you are building a fat jar and use the ProtectionDomain way you may hit some issues because you are loading the whole jar!
class.getProtectionDomain().getCodeSource().getLocation().toExternalForm();
So the better solution is the other provided solution
contextHandler.setResourceBase(
YourClass.class
.getClassLoader()
.getResource("WEB-INF")
.toExternalForm());
The problem here is if you are building a fat jar you are not really dumping your webapp resources into WEB-INF but are probably going into the root of the jar, so a simple workaround is to create a folder XXX and use the second approach as follows:
contextHandler.setResourceBase(
YourClass.class
.getClassLoader()
.getResource("XXX")
.toExternalForm());
Or change your build tool to export the webapp files into that given directory. Maybe Maven does this on a Jar for you but gradle does not.
Not unusually, I found a solution to my problem. The 3rd approach mentioned by Stephen in Embedded Jetty : how to use a .war that is included in the .jar from which Jetty starts? worked!
So, I changed from Resource_handler to WebAppContext, where WebAppContext is pointing to the same jar (testJetty.jar) and it worked!
String webDir = MainTest.class.getProtectionDomain()
.getCodeSource().getLocation().toExternalForm(); ; // Results in D:/Work/eclipseworkspace/testJettyResult/testJetty.jar
WebAppContext webappContext = new WebAppContext(webDir, "/");
It looks like ClassLoader.getResource does not understand an empty string or . or / as an argument. In my jar file I had to move all stuf to WEB-INF(any other wrapping dir will do). So the code looks like
contextHandler.setResourceBase(EmbeddedJetty.class.getClassLoader().getResource("WEB-INF").toExternalForm());
so the context looks like this then:
ContextHandler:744 - Started o.e.j.w.WebAppContext#48b3806{/,jar:file:/Users/xxx/projects/dropbox/ui/target/ui-1.0-SNAPSHOT.jar!/WEB-INF,AVAILABLE}

Meteor reading CSV files to populate database don't work after deploy

So I have a bunch of data that I want to load into database from CSV. I've hacked together a solution that works in local development, but when I deploy to meteor.com, it no longer works.
I'm loading the csv file in the folder /server/data/:
function readData(name){
var fs = __meteor_bootstrap__.require('fs');
var path = __meteor_bootstrap__.require('path');
var base = path.resolve('.');
var data = fs.readFileSync(path.join(base, '/server/data/', name));
return CSVToArray(data);
}
After I deploy to meteor.com, i got:
INFO Error: ENOENT, no such file or directory '/meteor/containers/98eb1286-120b-ee84-8e98-ce673fa2eab7/public/data/categories.csv'
at Object.openSync (fs.js:240:18)
at Object.readFileSync (fs.js:128:15)
at readData (app/server/models.js:10:16)
at app/server/categories.js:6:7
at /meteor/containers/98eb1286-120b-ee84-8e98-ce673fa2eab7/bundle/server/server.js:132:63
at Array.forEach (native)
at Function.<anonymous> (/meteor/containers/98eb1286-120b-ee84-8e98-ce673fa2eab7/bundle/server/underscore.js:76:11)
at /meteor/containers/98eb1286-120b-ee84-8e98-ce673fa2eab7/bundle/server/server.js:132:7
Any idea how I can get meteor to see the csv file after deployment?
I realize this question is old, but it still ranks high on certain keyword searches. So, if you're using Meteor 0.6.5+, you can use the new Assets API.
The issue is that meteor only bundles files that it knows about (ie. JS/CSS/HTML/+more depending on which packages you use) up when it deploys.
Try putting the file you need in the public directory (this directory is exempt from the above rule).
Thanks to SamuelDavis and Tom Coleman's tips. I ended up figuring out what the problem is. Turns out the bundled app is no longer formated as client, public, and server. I ended up debugging it by running meteor bundle to create a tarball. extract the tarball and took a look inside to find where the data folder is. Tom was also right that the data folder needed to be in the public folder in order to get bundled in.
It appears that the base directory is not in the same location that contains the file '/server/data/xxx.csv'.
Before you try anything else, log the base path after calling "var base = path.resolve('.'). If that value is what you expected, log the files that appear in that directory. Again if the files are what you expected, navigate into the /server folder and print out those directories and so forth.
This should pinpoint you to which folder and/or directory is missing and should indicate where you should place the CSV file in future.

Resources