Looping through all images within multiple directories in ColdFusion - loops

I'm working on an image manipulation script to create smaller thumbnail images of the images that are already on the server.
The directory structure that I need to search through is as follows:
Content
-Att1
-image1
-imgA1_1.png
-imgA1_1_large.png
-image2
-imgA1_2.png
-imgA2_large.png
-image3
-imgA1_3.png
-imgA1_3_large.png
-Att2
-image1
-imgA2_1.png
-imgA2_1_large.png
-image2
-imgA2_2.png
-imgA2_2_large.png
-image3
-imgA2_3.png
-imgA2_3_large.png
-Att3
-image1
-imgA3_1.png
-imgA3_1_large.png
-image2
-imgA3_2.png
-imgA3_2_large.png
-image3
-imgA3_3.png
-imgA3_3_large.png
etc...
So what I would like to to is be able to loop through all of the images shown above and if the dimensions of that image exceed 500-500 then create a thumbnail that is 100-100.
Is there any way of doing this without looping through each directory?

cfdirectory has a recurse attribute. This will loop through all the folders, but it won't require you to write code to loop through all the folders
<cfdirectory directory="yourDirectory" recurse="yes">

If you are dealing with very large directories cfdirecotry ends up being very slow. Here is a function that uses java that I wrote since we have some directories that have 1000s of images. It looks through and creates a query result for all your files and is MUCH faster
<cffunction name="getDirectorylisting" returntype="query" output="true">
<cfargument name="dirName" type="string" required="true" />
<cfargument name="recurse" type="boolean" default="false" required="false" />
<cfargument name="dirInfo1" type="query" default="#queryNew('datelastmodified,name,size,type,directory,hidden,pathname')#">
<cfscript>
var thisFile = '';
var listFiles = '';
var pathToParse = trim(dirName);
var thisPath = '';
var relPath = '';
var theFileObj = '';
var isDir = '';
var isFile = '';
var thisSize = '';
var lastModified = '';
var isHidden = '';
var theType = '';
if (left(dirName,2) == 'c:' || left(dirName,2) == 'd:' || left(dirName,2) == 'e:' || left(dirName,2) == '\\'){
//do nothing path is already absolute
} else {
pathToParse = expandPath(pathToparse);
}
if(right(pathToParse,1) == '/' || right(pathToParse,1) == '\'){pathToParse = left(pathToParse,len(pathToParse)-1);}
if(right(dirName,1) == '/' || right(dirName,1) == '\'){dirName = left(dirName,len(dirName)-1);}
if (directoryExists(pathToParse)){
listFiles = createObject("java","java.io.File").init(pathToParse).list();
for (thisFile=1;thisFile<=arrayLen(listFiles);thisFile=thisFile+1){
queryAddRow(arguments.dirInfo1);
thispath = "#pathToParse#\#listFiles[thisFile]#";
relpath = "#dirName#/#listFiles[thisFile]#";
theFileObj = createObject("java","java.io.File").init(thispath);
isDir = theFileObj.isDirectory();
isFile = theFileObj.isFile();
thisSize = val(theFileObj.length());
lastModified = theFileObj.lastModified();
isHidden = theFileObj.isHidden();
theType = "dir";
if (isFile){theType = "file";}
querySetCell(arguments.dirInfo1,"datelastmodified", lastModified );
querySetCell(arguments.dirInfo1,"name", listFiles[thisFile] );
querySetCell(arguments.dirInfo1,"size", thisSize );
querySetCell(arguments.dirInfo1,"directory", pathToParse );
querySetCell(arguments.dirInfo1,"hidden", isHidden );
querySetCell(arguments.dirInfo1,"type", theType );
querySetCell(arguments.dirInfo1,"pathName", relPath );
if (arguments.recurse && isDir && !isHidden){
arguments.dirInfo1 = getDirectoryListing(relPath,true, arguments.dirInfo1);
}
}
}
return arguments.dirInfo1;
</cfscript>
</cffunction>

Related

How to replace Pipelet for ExportCustomerGroups in SFCC? (For removing Deprecated API usage)

function abc(){
var Site = require('dw/system/Site');
var utils= require('app_nars/cartridge/scripts/util/utils.js');
var mySite : String = (Site.getCurrent().getID() == "a") ? "" : "-" + Site.getCurrent().getID();
var customerGroupName : String ;
if (mySite == "A") {
customerGroupName = "A";
} else {
customerGroupName = "B";
}
var grpNam= utils.getGroup(customerGroupName);
var grpFileName = 'test';
/* No script api available for pipelet ExportCustomerGroups*/
var Pipelet = require('dw/system/Pipelet');
var PipeletExecutionResponse = new dw.system.Pipelet('ExportCustomerGroups').execute({
CustomerGroups: grpNam.iterator(),
ExportFile : grpFileName,
OverwriteExportFile:true
});
app.getView().render('path/templateName');
}
How can we replace the Pipelet ExportCustomerGroups here , i could see in documentation we can use job steps and there is no script replacement
You have to create with your own code, construct the xml needed while parsing the customers.[enter image description here][1]
Or you can use this step when configuring job if no custom code needed there:
[1]: https://i.stack.imgur.com/s1VQa.png

Validation xpages file download control

I have to do a validation in component file download Control, checking if any file is listed. Does anyone know how to do this validating. How can I get the object representing the xpages component and list the files by any of its methods?
here is some code I use to get hold on the upload via ssjs (if you want that)
var con = facesContext.getExternalContext();
var request:com.sun.faces.context.MyHttpServletRequestWrapper = con.getRequest();
var fileDataName = getClientId('ctrlUpload') ;
var map:java.util.Map = request.getParameterMap();
var fileData:com.ibm.xsp.http.UploadedFile = map.get(fileDataName);
if( fileData == null ){
growl.createGrowlMessage("<strong>Warning</strong> no file to upload selected","danger");
return;
}
var tempClientFile = fileData.getClientFileName();
var tempFile:java.io.File = fileData.getServerFile();
var filePath = tempFile.getParentFile().getAbsolutePath();
var correctedFile = new java.io.File(filePath+java.io.File.separator + tempClientFile );
var success = tempFile.renameTo(correctedFile);
var doc:NotesDocument = attachDoc.getDocument(true);
var rtFiles:NotesRichTextItem = null;
if(!(doc.hasItem("Files"))){
rtFiles = doc.createRichTextItem("Files");
}else{
rtFiles = doc.getFirstItem("Files");
}
rtFiles.embedObject(lotus.domino.local.EmbeddedObject.EMBED_ATTACHMENT,"",correctedFile.getAbsolutePath(), null);
correctedFile.renameTo(tempFile);
attachDoc is the reference to the data-binding I have set up:
<xp:this.data>
<xp:dominoDocument formName="fa_Attachment"
var="attachDoc">
</xp:dominoDocument>
</xp:this.data>

Add database in codeigniter dynamically

I am working on codeigniter app to allow user to add edit and modify database.
I want to repeat this code by loop:
$db['db1']['hostname'] = 'localhost';
$db['db1']['username'] = 'ts4l_wp13';
$db['db1']['password'] = 'O&3D6c(0zD70.^9';
$db['db1']['database'] = 'ts4l_wp13';
$db['db1']['dbdriver'] = 'mysql';
$db['db1']['dbprefix'] = '';
$db['db1']['pconnect'] = FALSE;
$db['db1']['db_debug'] = TRUE;
$db['db1']['cache_on'] = FALSE;
$db['db1']['cachedir'] = '';
$db['db1']['char_set'] = 'utf8';
$db['db1']['dbcollat'] = 'utf8_general_ci';
$db['db1']['swap_pre'] = '';
$db['db1']['autoinit'] = TRUE;
$db['db1']['stricton'] = FALSE;
I do this:
#$get_data = mysql_query("SELECT * FROM `database_name`");
while($getdata = mysql_fetch_assoc($get_data)){
$id='db'.$getdata['id'];
$iid="$id";
$db["$iid"]['hostname'] = 'localhost';
$db["$iid"]['username'] = $getdata['username'];
$db["$iid"]['password'] = $getdata['password'];
$db["$iid"]['database'] = $getdata['name'];
$db["$iid"]['dbdriver'] = 'mysql';
$db["$iid"]['dbprefix'] = '';
$db["$iid"]['pconnect'] = FALSE;
$db["$iid"]['db_debug'] = TRUE;
$db["$iid"]['cache_on'] = FALSE;
$db["$iid"]['cachedir'] = '';
$db["$iid"]['char_set'] = 'utf8';
$db["$iid"]['dbcollat'] = 'utf8_general_ci';
$db["$iid"]['swap_pre'] = '';
$db["$iid"]['autoinit'] = TRUE;
$db["$iid"]['stricton'] = FALSE;
}
and this block.
// Loading db and running query.
$CI = &get_instance();
$this->db1 = $CI->load->database('db1', TRUE);
$this->db1->set($data);
$this->db1->insert($this->_table_name);
$id=$this->db1->insert_id();
I do this:
$this->load->model('mdb_m');
$dbms =$this->mdb_m->get();
if (count($dbms)) {
foreach ($dbms as $dbm) {
$mid=$dbm->id;
$dbc='db'.$mid;
$CI = &get_instance();
$this->db.$mid = $CI->load->database( "$dbc" , TRUE);
$this->db.$mid->set($data);
$this->db.$mid->insert($this->_table_name);
}
}
But it only insert at first database in the array then stop and retrive this error "You have specified an invalid database connection group."
you can get brief information of using multiple database from here.As you have mentioned.
https://ellislab.com/codeigniter/user-guide/database/connecting.html
for better approach you can make a loader file to load a database.I was in same problem and got the clear visualization from here
https://coderwall.com/p/_kyjvg/codeigniter-loading-up-multiple-databases
refer here and if still confused please revert back to me thankyou.

AS3/Flash: XML to array

How may I populate an array formated like this?:
var names:Array = [{label:"JAMES"}, {label:"JANE"}, {label:"JAMEL"}...];
From a XML like this one?:
<a>
<ap>
<name>JAMES</name>
<age>36</age>
</ap>
</a>
This is for AutoComplete components.
UPDATE, to start with something more suitable to my skills. THIS:
<list>
<label>ALPHA</label>
<label>ALLAN</label>
<label>ANTARES</label>
<label>...</label>
</list>
TO THIS:
var list:Array = [{label:"ALPHA"}, {label:"ALLAN"}, {label:"ANTARES"}...];
const xml:XML =
<list>
<label>ALPHA</label>
<label>ALLAN</label>
<label>ANTARES</label>
<label>...</label>
</list>;
const list:Array = new Array();
//xml..label.(trace(text()));
xml..label.(list.push({label:text()}));
// now we have an array:
// [{label:"ALPHA"}, {label:"ALLAN"}, {label:"ANTARES"}, {label:"..."}]
I recommend to read the AVM2 specification and to pay special attention to namespaces. Seriously. It's interesting and it's fun!
Hmmm... Alternative boring way:
const list:Array = new Array();
const xml:XML =
<list>
<label>ALPHA</label>
<label>ALLAN</label>
<label>ANTARES</label>
<label>...</label>
</list>;
const labels:XMLList = xml..label;
for each(var node:XML in labels)
{
trace(node);
var arrayItem:Object = new Object();
arrayItem.label = node.text(); // or node.toString() or .toJSON() or .to...
arrayItem.name = node.name();
// added only for debug-trace:
arrayItem.toString = function():String
{
var result:String = '{', delimiter:String = '';
for(var key:String in this)
if(key !== 'toString')
result += delimiter + key + ':"' + this[key] + '"',
delimiter ||= ', ';
return result + '}';
}
// add item to list:
list.push(arrayItem);
}
trace(list);

Anyone know how to proper use the nsIFilePicker ?

I am trying to copy a text file from one folder to another. Issue is once you select the folder to save to what is the proper code to get the file to copy to that folder? I and using NSI Filepicker modeOpen and modeSave and can't find any code on how to save file properly. the MDN is lacking code.
var dispdir = Components.classes["#mozilla.org/file/directory_service;1"].
getService(Components.interfaces.nsIProperties).
get("ProfD", Components.interfaces.nsIFile);
var nsIFilePicker = Components.interfaces.nsIFilePicker;
var fp = Components.classes["#mozilla.org/filepicker;1"]
.createInstance(nsIFilePicker);
fp.init(window, "Select a File", nsIFilePicker.modeOpen);
fp.appendFilters(nsIFilePicker.filterText);
fp.displayDirectory = dispdir;
var rv = fp.show();
if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnCancel) {
var file = fp.file;
var path = fp.file.path;
}
var savedir = Components.classes["#mozilla.org/file/directory_service;1"].
getService(Components.interfaces.nsIProperties).
get("ProfD", Components.interfaces.nsIFile);
savedir.append("Test Folder");
if( !savedir.exists() || !savedir.isDirectory() ) {
// if it doesn't exist,create
savedir.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777);
alert(savedir.path + "\n" + "Folder was made");
}
var fp2 = Components.classes["#mozilla.org/filepicker;1"]
.createInstance(nsIFilePicker);
fp2.init(window, "Save file to?", nsIFilePicker.modeSave);
fp2.appendFilters(nsIFilePicker.filterText);
fp2.displayDirectory = savedir;
fp2.defaultString = fp.file.leafName;
var rv = fp2.show();
if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {
}
var aDir = Components.classes["#mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
aDir.append(fp2.file.parent.path);
alert(fp2.file.parent.path)
fp.file.copyTo(aDir, null);
copyFile(fp.file.path);
alert(fp2.file.path + "\n" + "File copied successfuly!")
Use fp.file to create a stream, see
https://developer.mozilla.org/en/Code_snippets/File_I%2F%2FO#Synchronous
I have a question for you though, where is 'window' from fp.init(window defined?

Resources