I have a folder in my d drive.files in this drive i am using in my controller.so in controller i am specifying tht folder name for to each file recurse.But after development files may be saved in any other drive.so I want have environment variable for it.how to do that
It sounds like a configuration parameter would be better than an environment variable. Config.groovy is environment aware, so you can specify different values for development, production, etc.
environments {
production {
fileLocation = "D:/"
}
development {
fileLocation "/somewhere/else"
}
test {
fileLocation "/production/somewhere"
}
}
You can read the value of this parameter with:
def fileLocation = org.codehaus.groovy.grails.commons.ConfigurationHolder.config?.fileLocation
in grails 1.4 ConfigurationHolder is deprecated, so you should read config parameters using this instead:
def fileLocation = grailsApplication.config.fileLocation
Related
I have problem with date format in laravel model for different operating system (windows & linux) SQL Server
How to get the value from env to model, I create variable in .env for set condition in model when value variable environment 1 = windows and 2 = linux
// 1 FOR WINDOWS
// 2 FOR LINUX SERVER
ENVIRONMENT=1
Any solution / advice for this case?
All variables in your .env files are parsed as strings, so some reserved values have been created to allow you to return a wider range of types from the env() function.
make sure you already execute this command to clear your config after you added that variable to your config.
php artisan config:clear
Retrieving Environment Configuration
<?php
$environment = env("environment", 1)
?>
The second value passed to the env function is the "default value". This value will be used if no environment variable exists for the given key.
see docs here env
in Laravel function env() returns values from .env file.
So if you have ENVIROMENT = 1 in your .env, using env('ENVIRONMENT'), you will get that value.
I'm putting together a AngularJS+Typescript+VisualStudio project. I want to have a configuration file with constants in it that control different settings (e.g., REST API URLs and environment names). How is this typically done in this kind of project?
I might have a dev config file called app.dev.config.ts like this:
module app.config {
export class ConfigSettings {
static environment(): string { return "dev"; }
static dataApiBaseUrl(): string { return "DevDataService"; }
}
}
and an app.prod.config.ts like this:
module app.config {
export class ConfigSettings {
static environment(): string { return "prd"; }
static dataApiBaseUrl(): string { return "PrdDataService"; }
}
}
Of course this doesn't actually work because these two classes have the same name.
I need to set this up in a way so that I build this only once in my build server, and then can deploy this to a fixed (3) number of environments. Maybe this means that when I go to deploy this to some environment, I have an additional step where I rename a config file. This is what I do for C# projects and their config files.
I've searched around online for this, but all I can find is references to tsconfig.json files.
I found a solution for this.
1) I put together separate config files like env.dev.ts and env.prd.ts in my project. Their contents look like this:
angular.module('compdb') //module name matches my main module
.constant('env', 'prd')
.constant('key1', 'prd value 1')
.constant('key2', 'prd value 2');
2) Visual Studio transpiles these to env.dev.js, etc.
3) In my gulp file, I copy the env.*.js files to my output directory.
4) In my Index.cshtml file, I include env.js. I include this after my scripts that create the compdb angular module
5) When I deploy my code to any environment, I rename the appropriate config file (e.g., env.prd.js) to env.js
For example I want to test my method on my local environment with the DB from production environment. Is it possible to set/change env(local/staging/production) in the method or before method in specific controller?
Using CakePHP version 2.2.9
Yes...!!!
you can make a condition in your configure file
Make a individual path of your Local/Staging/Production folder
$servername = $_SERVER['SERVER_NAME'];
if($servername == 'localhost'){
define('SITE_PATH','http://YourLocalWebsite');
} else if($servername == 'Dev.yourwebsite') {
define('SITE_PATH','http://StagingYourwebsite');
} else {
define('SITE_PATH','http://YourWebsite');
}
How do I write a Google Apps Script that deletes files?
This finds files:
var ExistingFiles = DocsList.find(fileName);
But DocsList.deleteFile does not exist to delete a file.
Is there a way to move those files to another Folder or to Trash?
The other workaround I would consider is to be able to override an existing file with the same name.
Currently when I want to create a file with a name already used in MyDrive then it creates a second file with the same name. I would like to keep 1 file (the new one is kept and the old one is lost).
There are 3 services available to delete a file.
DriveApp - Built-in to Apps Script
Advanced Drive Service - Built-in to Apps Script but must be enabled. Has more capability than DriveApp
Google Drive API - Not built-in to Apps Script, but can be used from Apps Script using the Drive REST API together with UrlFetchApp.fetch(url,options)
The DocsList service is now deprecated.
The Advanced Drive Service can be used to delete a file without sending it to the trash. Seriously consider the risk of not being able to retrieve the deleted file. The Advanced Drive Service has a remove method which removes a file without sending it to the trash folder. Advanced services have many of the same capabilities as the API's, without needing to make an HTTPS GET or POST request, and not needing an OAuth library.
function delteFile(myFileName) {
var allFiles, idToDLET, myFolder, rtrnFromDLET, thisFile;
myFolder = DriveApp.getFolderById('Put_The_Folder_ID_Here');
allFiles = myFolder.getFilesByName(myFileName);
while (allFiles.hasNext()) {//If there is another element in the iterator
thisFile = allFiles.next();
idToDLET = thisFile.getId();
//Logger.log('idToDLET: ' + idToDLET);
rtrnFromDLET = Drive.Files.remove(idToDLET);
};
};
This combines the DriveApp service and the Drive API to delete the file without sending it to the trash. The Drive API method .remove(id) needs the file ID. If the file ID is not available, but the file name is, then the file can first be looked up by name, and then get the file ID.
In order to use DriveAPI, you need to add it through the Resources, Advanced Google Services menu. Set the Drive API to ON. AND make sure that the Drive API is turned on in your Google Cloud Platform. If it's not turned on in BOTH places, it won't be available.
Now you may use the following if the file is as a spreadsheet, doc etc.:
DriveApp.getFileById(spreadsheet.getId()).setTrashed(true);
or if you already have the file instead of a spreadsheet, doc etc. you may use:
file.setTrashed(true);
This code uses the DocsList Class which is now deprecated.
try this :
function test(){
deleteDocByName('Name-of-the-file-to-delete')
}
function deleteDocByName(fileName){
var docs=DocsList.find(fileName)
for(n=0;n<docs.length;++n){
if(docs[n].getName() == fileName){
var ID = docs[n].getId()
DocsList.getFileById(ID).setTrashed(true)
}
}
}
since you can have many docs with the same name I used a for loop to get all the docs in the array of documents and delete them one by one if necessary.
I used a function with the filename as parameter to simplify its use in a script, use test function to try it.
Note : be aware that all files with this name will be trashed (and recoverable ;-)
About the last part of your question about keeping the most recent and deleting the old one, it would be doable (by reading the last accessed date & time) but I think it is a better idea to delete the old file before creating a new one with the same name... far more logical and safe !
Though the The service DocsList is now deprecated, as from the Class Folder references, the settrashed method is still valid:
https://developers.google.com/apps-script/reference/drive/folder#settrashedtrashed
So should work simply this:
ExistingFiles.settrashed(true);
Here is another way to do it without the need of Drive API. (based on Allan response).
function deleteFile(fileName, folderName) {
var myFolder, allFiles, file;
myFolder = DriveApp.getFoldersByName(folderName).next();
allFiles = myFolder.getFilesByName(fileName);
while (allFiles.hasNext()) {
file = allFiles.next();
file.getParents().next().removeFile(file);
}
}
Here is a slightly modified version using the above. This will backup said file to specified folder, also remove any old previous backups with the same name so there are no duplicates.
The idea is here to backup once per day, and will retain 1 month of backups in your backup folder of choice. Remember to set your trigger to daily in your Apps Script.
https://gist.github.com/fmarais/a962a8b54ce3f53f0ed57100112b453c
function archiveCopy() {
var file = DriveApp.getFileById("original_file_id_to_backup");
var destination = DriveApp.getFolderById("backup_folder_name");
var timeZone = Session.getScriptTimeZone();
var formattedDate = Utilities.formatDate(new Date(),timeZone,"dd"); // 1 month backup, one per day
var name = SpreadsheetApp.getActiveSpreadsheet().getName()+"_"+formattedDate;
// remove old backup
var allFiles = destination.getFilesByName(name);
while (allFiles.hasNext()) {
var thisFile = allFiles.next();
thisFile.setTrashed(true);
};
// make new backup
file.makeCopy(name,destination);
}
I have developed a grails app which has user file uploads (docs, etc..), they are stored in the relative folder "web-app/upload".
My question is that I do not know what is the best way to perform automatically war deployments and keep this folder. Because when I redeploy in Tomcat the whole app folder is deleted and all the files are deleted.
Additionaly I need a generic configuration fron set an external location from this Files
Have you found a solution for that?
P.D.: If I use System.properties['base.dir'] the result is null, and if I use a ApplicationHolder.application.mainContext.getResource() it return a temp path. :(
You should not be uploading files into your WAR structure. You should upload them to some external location.
I was able to solve partial as follow
//for development environment
def root = System.properties['base.dir']?.toString()
if(!root){
//for production environment in war deplements
def tmpRoot = ApplicationHolder.application.mainContext.getResource('WEB-INF').getFile().toString()
root = tmpRoot.substring(0, tmpRoot.indexOf(File.separator + 'temp' + File.separator))
}
if(!root){
throw new Exception('Not found a valid path')
}
return root + File.separator
I hope it can be useful to others
Regards,
Yecid PacĂfico
This code obtains the parent folder where the application is located:
String path = servletContext.getRealPath("/");
String parentStr = new File(path).getParentFile().getParent();
I mean, if the web application were located in D:\somefolder\myWeb
path would be D:\somefolder\myWeb\web-app
parentStr would be D:\somefolder
So you could save the files in D:\somefolder\files-outside-myWeb-context
Is it what you are looking for?