how to specify drupal mail save file module's directory path? - drupal

I have installed above mentioned module.
It works fine, my doubt is how to mention current year in file directory field. Since, I would like to get mails saved in sub directories in format : year/month/date

Try writing your own hook_file_presave() function and hook file saving path:
https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_file_presave/7.x
See what you have in that $file variable passed to the function, try somehow to detect that this module is saving the file and in that case change file path the way you want.

Related

Unable to delete uploaded XML file on Drupal 8.5

I have created a media type that accepts XML files and saves them to a custom publicly accessible location on the server.
Ideally I would like the file to be overwritten when the exact same file is uploaded. This does not happen, instead it creates a new file and adds a number on the end. I have "Create new Revision" turned off.
To get around this issue I thought I could just delete the file via the CMS. The uploaded file has status of "Permanent" and is used 0 places. I know the cron job cleans up files for you, but when I run the cron the file in question is still there. I figure it's because the file is set to permanent, but I don't see a way to flip this to temporary.
Any help is much appeciated.
There is a setting nested away in the file system settings, which lets you configure it to remove (or not removed) orphaned files. If drush isn't removing them despite having no usages recorded, I'd check this option isn't ticked.
The temporary and permanent status are used for storing temporary files during the upload/save process, so I wouldn't tinker with those too much.
If you fancy making the form yourself using the form API, then you can save the file programmatically using the FILE_EXISTS_REPLACE parameter.
https://api.drupal.org/api/drupal/core%21modules%21file%21file.module/function/file_save_data/8.5.x

How to define Wordpress global variable for use in a REST Controller?

I'm extending the WP REST API by writing a Controller class.
I'm trying to read the config for this class from a file, e.g:
{
"base-namespace": "myapi",
"version": "v1",
"resource": "things"
}
This would allow me to keep server and client in sync as they would both use the same config file.
However, I do not want WP to stay reading this file for every request it serves... Currently, if I read this file from anywhere in the plugin file (or any of its required files - including the Controller definition), and if I also echo out where I'm reading, I see it's always passing through that bit of code (including the reading) for every request.
I imagine I need to read this file somewhere outside the plugin itself - make it a global, and then access it when instantiating the Controller.
I'm new to WP - this is the first time dabbling with it. Where should this global variable definition go such that it's only executed once?
Note:
I have tried using require_once in my plugin to require a config file which does the file reading. I had put an echo statement there and it shows that that file gets executed for every request (despite the require_once).
I have also tried wrapping the file reading in an if(!isset($my_global_var) statement. But adding an echo statement inside the if statement shows that this global variable is always unset with every request served... clearly this needs to go in some kind of WP startup file which only gets executed once.
Thank you.
Store your config data as a PHP array in a .php file and then include it using the PHP include statement. Advanced PHP engines parse the PHP source once and cache a compiled representation of the script so that it does not have to re-parse the PHP sources everytime. So if your data is inside a PHP source file it would be saved in the PHP's engine compiled script cache.
Of course if your client is non-PHP it would need to have code to parse a PHP array.

How to get relative path to a file in cache

I'm trying to get the relative path to a file inside a app/cache dir.
I've tried via custom config file using:
parameters:
output_dat_path : %kernel.root_dir%/cache/%kernel.environment%/myfile.dat
Also I tried:
$kernel = $this->get('kernel');
$path = $kernel->getRootDir() . '/cache/' . $kernel->getEnvironment() . 'myfile.dat';
Both ways generate absolute paths, that both work fine using them inside a controller or service, but, I need a relative path to generate a link for the user.
I'm thinking to move the file to the web directory in my service and use asset in the template to generate the link, but the file is private for every user and I don't want to expose it.
Any thought?
Thanks in advance.
UPDATE
I solved this as follows:
User, once authenticated, generates his file. If the file doesn't exists, is generated. If it exists, the controller checks the user rights, time, etc. and returns the file.
If the system has to be generated, the file is created in a private folder and an encrypted name is generated. The encrypted name is generated using base_64 and some changes after that.
The system shows to the user a link. That link is something like /get/privatefile/eDfa...23Hn
When the user clicks on it, the controller checks the user, time from creation, etc.
If all is fine the controller returns the file.
I made this because the file must be created once, and be available for a short period of time and after must be deleted. If an user pass the link to other person, the checksum of the encrypted name fails and the file cannot be accessed.
I am sure that this is not the best solution but it works right know.

selecting path in chrome.fileSystem.chooseEntry

I'm trying to open a file in a Chrome packaged-app using chrome.fileSystem.chooseEntry() but I can't manage to make the dialog show a suggested filename (passed through suggestedName). That works ok if the file is in home path, but doesn't if I 'suggest' a file that's inside a folder in that same home folder. I'm on LinuxMint.
So:
chrome.fileSystem.chooseEntry({type: 'openFile', suggestedName: "~/log.txt"}, callback) ---> works! but,
chrome.fileSystem.chooseEntry({type: 'openFile', suggestedName: "~/foo/log.txt"}, callback) ---> doesn't! It just opens dialog showing home folder. Both file and folder exist.
This is by design. The file system API strips path information from the suggested name. Put another way, you can suggest a name but not a path.
We do this for security reasons; we want users to always be choosing the file. If your app has a legitimate reason to suggest a path please file a bug at http://crbug.com, describe your use case and cc me.

drupal 7 error when uploading with new field

I've got a very strange problem using drupal 7. I've got a wbesite running and using views and content types I have been able to build a lot of functionality. Now the following occurs:
When I add a new field to a content type (new or existing) of the the type file or image, I have this field in the form but when I try to upload a file or image using this form it gives an error "The file could not be uploaded" or the full error (from the recent log messages): "The upload directory public:// for the file field field_katapult_voorpagina could not be created or is not accessible. A newly uploaded file could not be saved in this directory as a consequence, and the upload was canceled."
Strangely, when I upload a file or image using an older existing field (pointing to the same directory: public://) it does not give an error and the files upload without a problem. So this makes me think that the server settings are actually set properly.
Anybody who can help me with this problem?
If not, can anybody point me in the right direction as to where I can find the files that handle the upload process in drupal 7?
Many thanks in advance!
You need to check a couple of things :
where is your public files folder, and if that is writable by the webserver ( in /admin/config/media/file-system )
where does your working field send files (it might be a different directory), and if it is, use that for the new fields as well.
Check you file destination directory setting of new image field. Do not include preceding or trailing slashes. This field supports tokens.
You can also check max, min resolutions, file upload limit, image style (if it is new image style other than thumbnail/ or existing) & number of values ( if it set to 1 you can't upload 2 or more). Finally you have to set it public files type.
I think you should check the files folder permission.

Resources