Thursday, March 17, 2011 at 10:04 AM.

scripting2Suite.writeStaticFile

on writeStaticFile (username, filepath, filetext) {
	<<Changes
		<<3/16/11; 6:28:54 PM by DW
			<<If this is not the user who blogs in the root directory, add the username at the beginning of the path.
		<<3/16/11; 12:22:49 PM by DW
			<<Rewrite to use builtins.staticText if it's enabled. Otherwise use the original method. Provides an easy way to transition. 
		<<3/8/11; 6:45:44 PM by DW
			<<Optionally write to S3.
		<<8/25/10; 9:58:09 AM by DW
			<<Add code for writing to a local folder.
		<<8/20/10; 9:13:48 AM by DW
			<<Don't fail if the closeConnection call fails.
		<<5/29/10; 4:41:09 PM by DW
			<<Created. 
	local (adrdata = scripting2Suite.init (), now = clock.now ());
	if adrdata^.prefs.statictext.prefs.enabled { //new method -- 3/16/11 by DW
		local (adruser = scripting2Suite.initUser (username));
		if not adruser^.prefs.flBlogInRootDirectory {
			if filepath beginswith "/" {
				filepath = string.delete (filepath, 1, 1)};
			filepath = username + "/" + filepath};
		return (staticText.write (@adrdata^.prefs.statictext, filepath, filetext))}
	else {
		local (adrdata = scripting2Suite.init (), url, now = clock.now ());
		local (adruserdata = scripting2Suite.initUser (username));
		<<scratchpad.wsusername = username; scratchpad.wsfilepath = filepath; scratchpad.wsfiletext = filetext;
		if not (filepath beginswith "/") {
			filepath = "/" + filepath};
		if adruserdata^.prefs.localFolder.enabled {
			local (basefolder);
			bundle { //set basefolder
				if adruserdata^.prefs.localFolder.enabled {
					basefolder = adruserdata^.prefs.localFolder.folder;
					url = adruserdata^.prefs.localFolder.url}
				else {
					basefolder = adrdata^.prefs.localFolder.folder + username;
					url = adrdata^.prefs.localFolder.folder + username + "/"}};
			if filepath beginswith "/" {
				filepath = string.delete (filepath, 1, 1)};
			local (f = basefolder + string.replaceall (filepath, "/", file.getpathchar ()));
			file.surefilepath (f);
			file.writewholefile (f, filetext);
			url = url + filepath};
		if adruserdata^.prefs.s3.enabled { //3/8/11 by DW
			local (s3path = filepath);
			if s3path beginswith "/" {
				s3path = string.delete (s3path, 1, 1)};
			s3path = adruserdata^.prefs.s3.path + s3path;
			try {
				adruserdata^.stats.s3.lastUrlWritten = s3.newobject (s3path, filetext)}
			else {
				adruserdata^.stats.s3.ctErrors++;
				adruserdata^.stats.s3.whenLastError = now;
				adruserdata^.stats.s3.lastError = tryerror};
			adruserdata^.stats.s3.whenLastWrite = now;
			adruserdata^.stats.s3.ctWrites++};
		if adrdata^.prefs.ftp.enabled {
			local (server = adrdata^.prefs.ftp.server);
			local (ftpusername = adrdata^.prefs.ftp.username);
			local (ftppassword = adrdata^.prefs.ftp.password, path);
			bundle { //set path
				if adruserdata^.prefs.ftp.enabled {
					path = adruserdata^.prefs.ftp.path}
				else {
					path = adrdata^.prefs.ftp.path + username};
				path = path + filepath};
				<<bundle //old code
					<<path = adrdata^.prefs.ftp.path + username + filepath
			local (connection = tcp.ftp.openConnection (server, ftpusername, ftppassword, false));
			if path beginswith "/" {
				path = string.delete (path, 1, 1)}; //6/9/10 by DW
			tcp.ftp.writefile (connection, filetext, path);
			thread.callscript (@tcp.ftp.closeConnection, {connection});
			bundle { //build the url
				if adruserdata^.prefs.ftp.enabled {
					url = adruserdata^.prefs.ftp.url;
					if url endswith "/" {
						url = string.mid (url, 1, sizeof (url) - 1)};
					url = url + filepath}
				else {
					url = adrdata^.prefs.ftp.baseurl + username + filepath}}};
		return (url)}}; //8/25/10 by DW
bundle { //test code
	<<url = writeStaticFile (scratchpad.wsusername, scratchpad.wsfilepath, scratchpad.wsfiletext)
	url = writeStaticFile ("davewiner", "testing/letshavefun.txt", clock.now ());
	webbrowser.openurl (url)}



This listing is for code that runs in the OPML Editor environment. I created these listings because I wanted the search engines to index it, so that when I want to look up something in my codebase I don't have to use the much slower search functionality in my object database. Dave Winer.