Jump to content
Server Maintenance This Week. ×

Container > NAS drive w/Server Script


This topic is 3702 days old. Please don't post here. Open a new topic instead.

Recommended Posts

I have my email attachments in a child table to the parent email record, each in a container field and use a server-side script and 360s email plug-in to send the email (EmailAttachFile (myContainer) ).

However, we want to save the files in the containers out to a NAS drive after Send.

Server-side script does not support Export Field Contents, and so, I'm stuck. Is there a SM function that'll help overcome this obstacle?

tia,

Barbara

Link to comment
Share on other sites

I don't know of a function that comes with ScriptMaster, but by looking at "Get File As Container", it seems like a Groovy File object is interchangeable with a FileMaker container field. Perhaps you could reverse the logic and create a function that writes the contents of a container field to file. You might also want to look at the function "Write To File".

Link to comment
Share on other sites

Thanks, Dan. Yes, saw the Get File as Container and thought, hmm..all I need is the reverse. Thanks, I'll look at Write to File as well.

I might need to resort to a Robot.

But..not giving up, 360?

Link to comment
Share on other sites

I couldn't sleep, so I thought I'd take a swing at this. After digging around in ScriptMaster Documentation, I noticed the fmpro.getContainerStream() function. I have a basic sample working with the code below, but it doesn't do any error checking.


// SaveContainerAsFile(containerField, path)

// container field's filename is appended to the path parameter



container = fmpro.getContainerStream(containerField)

fileName = fmpro.evaluate(containerField)

File f = new File( path + fileName )

f.append( container )

I'm not so sure about the 'f.append' part; I would assume there is a better method than append, but that was the first one I found that worked, so I used it.

Link to comment
Share on other sites

Whoa! Although I use SM a lot, I'm certainly not an expert. If this works -- you've saved me from a Robot. Many thanks.

However, I don't want to close this thread until 360 chimes in....

Link to comment
Share on other sites

I forgot to mention that the containerField parameter needs to be the field name as text. Try the attached file; it worked for me. (I'm on windows too)

As I said before, there is no error checking built into this. What I can think of off the top of my head that should/could be checked for is...

  • path parameter ends with a separator
  • container field exists (would have given you a usefull error when you passed the container data, rather than name of container field)
  • path is a valid path
  • path is writeable
  • file exists after attempting to create it

I know you can get a few of these test's from other ScriptMaster functions; path is valid/writeable for example. I know I've seen those tests in other functions.

Regarding using the .append method of the File object; I really don't think this is the correct way to do it. This link talks about saving an InputStream to a file: http://www.roseindia.net/java/java-conversion/InputstreamToFile.shtml

fmpro.getContainerStream() function returns an InputStream

(or throws com.prosc.fmkit.FmCalculationException or java.io.IOException)

ContainerTest_rev1.zip

Link to comment
Share on other sites

Bridget

It is the path that is the problem

You need an OS version of it as you are using some groovy

Get ( DesktopPath ) returns /MacBookPro/Uses/RWU/Desktop (on my machine)

and the path you need to feed the function needs to be/Users/RWU/Desktop/filename

So OS path + separator + filename

Here is a working version that ask for the directory to save in then goes and saves it there - this takes the one parameter of the container field as a fileName and another of overwrite, if that is true (1) it will overwrite the existing file othewise return a message saying it exists


import javax.swing.JFileChooser;



JFileChooser chooser = new JFileChooser()

chooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY )



if( chooser.showOpenDialog( null ) == JFileChooser.APPROVE_OPTION ) {

	path =  chooser.getSelectedFile().getAbsolutePath();

} else return 'ERROR'



sep = System.getProperty("file.separator")

container = fmpro.getContainerStream(containerField)

fileName = fmpro.evaluate(containerField)

f = new File( path + sep + fileName )



if (f.exists() == false || overwrite == "1" ){

f.append( container )

} else { 

return 'ALREADY exists'

}

Link to comment
Share on other sites

@Dan - thank you. I appreciate your help very much.

@John - thank you, too. However, this is for a server-script, and so I won't be asking for a folder destination. The destination directory will exist* and the filename will be a recordID (and so, unique). As for paths, I've been using Matt's CF to convert FM paths to OS paths for use with other SM functions. I'll try that here.

*The destination directory does need to be created if it doesn't exist. I can use SM for that.

Link to comment
Share on other sites

your welcome. I'm glad I could help.

Do you mind posting the code you come up with? I like the idea of just creating the directory if it does not exist, rather than just testing if it exists.

Link to comment
Share on other sites

I'm so pathetic at this that I'll string SM commands together to accomplish what I need. So, I'll test for the dir and if error, create it. I used to upload to FTP using the 360 plugin, and it was slick, in that if the dir didn't exist, it created it on upload. The dir name was derived from a calc, Floor ( __kP_DocID / 1000 )

Link to comment
Share on other sites

a function for doing that


// CreateFolder(fm_FilePath)

// 10_06_20 JR WORKING

// v1.1

//



//Version with decent error reporting:

dir = new File( filePath )

if( dir.exists() ) return false

if( dir.mkdirs() ) { 

	//Success!

	return true

} else { 

	//Figure out why it failed

	File realParent = dir

	while( realParent != null && ! realParent.exists() ) {

		realParent = realParent.getParentFile()

	}

	if( realParent != null && !realParent.canWrite() ) {

		throw new FileNotFoundException("Directory " + dir.getAbsolutePath() + " could not be created because the parent directory " + realParent.getAbsolutePath() + " is not writeable" )

	} else {

		throw new FileNotFoundException("Directory + " + dir.getAbsolutePath() + " could not be created because of an unknown error." )

	}

}

Link to comment
Share on other sites

John, isn't this the CreateFolder module that ships with SM? Have you modified it...I don't see any difference.

btw, it's Barbara, not Bridget.

Link to comment
Share on other sites

This will also work for files stored as reference.

Will overwrite existing file (this can easily be changed at the end of the code if you do NOT want that).

This code still uses the append() method. I will eventually look into the proper way of creating the file, here is another reference for that: http://download.oracle.com/javase/tutorial/essential/io/.


// ExportContainer(containerField, path, fileName)

//     containerField  must be the container field name as text; use getFieldName()

//     path            where to save file. Will be created if it does not exist

//                         (trailing slash can be used OR left off)

//     fileName        can be provided in three different formats:

//                         ""          name.ext of file in container field will be used

//                         "name"      ext of file in container field will be added to provided name

//                         "name.ext"  will be named exactly as provided





// get container data

InputStream container

try{

	container = fmpro.getContainerStream(containerField)

}catch(e){

	throw new Exception('Could not get data from container (make sure container field name is passed as text)')

}



// test if container field is empty

if( container == null ) {

	throw new Exception('Container field is empty')

}



// validate path

File output

try{

	output = new File( path )

	output.getCanonicalPath()	//this seems to validate the path

}catch(e){

	throw new Exception('Invalid parameter: could not be converted to a path: '+path)

}



// create directory(s) if it(they) doesn't exist

if( ! output.isDirectory() ){

	output.mkdirs()

}



// determine output file name

if( fileName == null ) {

	// get file name from container field

	fileName = fmpro.evaluate(containerField)

	

	// if length is 0, then file is stored as reference,

	//   and file name needs to be extracted differently

	if ( fmpro.evaluate('length(' + containerField + ')') == '0' ){

		// find everything after the last forward slash

		fileName = fileName.find('[^/]+$')

	}

} else if ( fileName.find('\\.') == null ) {

	// get file extenstion from container field

	containerAsText = fmpro.evaluate(containerField)

	fileName += containerAsText.find('\\.[^\\.]+$')

}



// add file name to path

output = new File( output.getCanonicalPath() + output.separator + fileName )



// delete file if it exists

if( output.exists() == true ){

	output.delete()

}



// create file

output.append( container )

Link to comment
Share on other sites

Dan! You're terrific. I will definitely try this out.

I ran into problems using your first version (the revised). What am I overlooking (beside a path error that I can't see)? Could it be SM versions or jar files that must be loaded?

----

And she's back. Very happy. Works a charm. I owe you!

Link to comment
Share on other sites

  • 2 years later...

This topic is 3702 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.