Skip to content

Commit

Permalink
Finalize all actions and tests for file/cffile component except uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
jclausen committed Apr 10, 2024
1 parent e07a535 commit 147e0d6
Show file tree
Hide file tree
Showing 4 changed files with 379 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ private void copy( IBoxContext context, String directory, String newDirectory, B
Key.filter, filter,
Key.createPath, createPath
);
System.out.println( argumentsMap.asString() );
actionsMap.get( Key.copy ).invoke( context, argumentsMap, false, Key.directoryCopy );
}

Expand Down
112 changes: 82 additions & 30 deletions src/main/java/ortus/boxlang/runtime/components/io/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,63 @@
*/
package ortus.boxlang.runtime.components.io;

import java.util.HashMap;
import java.util.Set;

import ortus.boxlang.runtime.BoxRuntime;
import ortus.boxlang.runtime.bifs.BIFDescriptor;
import ortus.boxlang.runtime.components.Attribute;
import ortus.boxlang.runtime.components.BoxComponent;
import ortus.boxlang.runtime.components.Component;
import ortus.boxlang.runtime.context.IBoxContext;
import ortus.boxlang.runtime.dynamic.ExpressionInterpreter;
import ortus.boxlang.runtime.scopes.Key;
import ortus.boxlang.runtime.types.IStruct;
import ortus.boxlang.runtime.types.exceptions.BoxRuntimeException;
import ortus.boxlang.runtime.util.FileSystemUtil;
import ortus.boxlang.runtime.validation.Validator;

@BoxComponent
public class File extends Component {

/**
* The runtime instance
*/
protected BoxRuntime runtime = BoxRuntime.getInstance();
private final Key fileAppendKey = Key.of( "fileAppend" );
private final Key fileCopyKey = Key.of( "fileCopy" );
private final Key fileDeleteKey = Key.of( "fileDelete" );
private final Key fileMoveKey = Key.of( "fileMove" );
private final Key fileReadKey = Key.of( "fileRead" );
private final Key fileReadBinaryKey = Key.of( "fileReadBinary" );
private final Key fileUploadKey = Key.of( "fileUpload" );
private final Key fileUploadAllKey = Key.of( "fileUploadAll" );
private final Key fileWriteKey = Key.of( "fileWrite" );

private final HashMap<Key, BIFDescriptor> actionsMap = new HashMap<Key, BIFDescriptor>() {

{
put( Key.append,
runtime.getFunctionService().getGlobalFunction( fileAppendKey ) );
put( Key.copy,
runtime.getFunctionService().getGlobalFunction( fileCopyKey ) );
put( Key.delete,
runtime.getFunctionService().getGlobalFunction( fileDeleteKey ) );
put( Key.move,
runtime.getFunctionService().getGlobalFunction( fileMoveKey ) );
put( Key.read,
runtime.getFunctionService().getGlobalFunction( fileReadKey ) );
put( Key.readBinary,
runtime.getFunctionService().getGlobalFunction( fileReadBinaryKey ) );
put( Key.upload,
runtime.getFunctionService().getGlobalFunction( fileUploadKey ) );
put( Key.uploadAll,
runtime.getFunctionService().getGlobalFunction( fileUploadAllKey ) );
put( Key.write,
runtime.getFunctionService().getGlobalFunction( fileWriteKey ) );

}
};

/**
* Constructor
*/
Expand All @@ -49,7 +91,7 @@ public File() {
new Attribute( Key.output, "string" ),
new Attribute( Key.addnewline, "boolean", false ),
new Attribute( Key.attributes, "string" ),
new Attribute( Key.charset, "string" ),
new Attribute( Key.charset, "string", "utf-8" ),
new Attribute( Key.source, "string" ),
new Attribute( Key.destination, "string" ),
new Attribute( Key.variable, "string" ),
Expand Down Expand Up @@ -105,41 +147,51 @@ public File() {
*
*/
public BodyResult _invoke( IBoxContext context, IStruct attributes, ComponentBody body, IStruct executionState ) {
Key action = Key.of( attributes.getAsString( Key.action ) );
String file = attributes.getAsString( Key.file );
String mode = attributes.getAsString( Key.mode );
String output = attributes.getAsString( Key.output );
Boolean addnewline = attributes.getAsBoolean( Key.addnewline );
String fileAttributes = attributes.getAsString( Key.attributes );
String charset = attributes.getAsString( Key.charset );
String source = attributes.getAsString( Key.source );
String destination = attributes.getAsString( Key.destination );
String variable = attributes.getAsString( Key.variable );
String filefield = attributes.getAsString( Key.filefield );
String nameconflict = attributes.getAsString( Key.nameconflict );
String accept = attributes.getAsString( Key.accept );
String result = attributes.getAsString( Key.result );
Boolean fixnewline = attributes.getAsBoolean( Key.fixnewline );
Double cachedwithin = attributes.getAsDouble( Key.cachedwithin );
Key action = Key.of( attributes.getAsString( Key.action ) );
String output = attributes.getAsString( Key.output );
String variable = attributes.getAsString( Key.variable );

if ( action.equals( Key.write ) ) {
write( context, file, output, addnewline, fileAttributes, charset, fixnewline, mode );
attributes.put( Key.data, output );
actionsMap.get( Key.write ).invoke( context, attributes, false, fileWriteKey );
} else if ( action.equals( Key.append ) ) {
attributes.put( Key.data, output );
actionsMap.get( Key.append ).invoke( context, attributes, false, fileAppendKey );
} else if ( action.equals( Key.copy ) ) {
actionsMap.get( Key.copy ).invoke( context, attributes, false, fileCopyKey );
} else if ( action.equals( Key.delete ) ) {
actionsMap.get( Key.delete ).invoke( context, attributes, false, fileDeleteKey );
} else if ( action.equals( Key.move ) || action.equals( Key.rename ) ) {
actionsMap.get( Key.move ).invoke( context, attributes, false, fileMoveKey );
} else if ( action.equals( Key.read ) ) {
if ( variable == null ) {
throw new BoxRuntimeException( "The [variable] attribute is required for file action [read]." );
}
attributes.put( Key.filepath, attributes.get( Key.file ) );
ExpressionInterpreter.setVariable(
context,
attributes.getAsString( Key.variable ),
actionsMap.get( Key.read ).invoke( context, attributes, false, fileReadKey )
);
} else if ( action.equals( Key.readBinary ) ) {
if ( variable == null ) {
throw new BoxRuntimeException( "The [variable] attribute is required for file action [readBinary]." );
}
attributes.put( Key.filepath, attributes.get( Key.file ) );
ExpressionInterpreter.setVariable(
context,
attributes.getAsString( Key.variable ),
actionsMap.get( Key.readBinary ).invoke( context, attributes, false, fileReadBinaryKey )
);
} else if ( action.equals( Key.upload ) ) {
throw new BoxRuntimeException( "The file action [upload] is not yet implemented in the core runtime" );
} else if ( action.equals( Key.uploadAll ) ) {
throw new BoxRuntimeException( "The file action [uploadAll] is not yet implemented in the core runtime" );
} else {
throw new BoxRuntimeException( "unimplemeted action: " + action );
}

return DEFAULT_RETURN;
}

private void write( IBoxContext context, String file, String output, Boolean addnewline, String fileAttributes, String charset, Boolean fixnewline,
String mode ) {
charset = charset == null ? "UTF-8" : charset;
output = addnewline ? output + "\n" : output;
if ( fixnewline ) {
output = output.replaceAll( "\r\n", java.io.File.separator );
}
// TODO: Apply attributes and mode
FileSystemUtil.write( file, output, charset, true );
}

}
5 changes: 5 additions & 0 deletions src/main/java/ortus/boxlang/runtime/scopes/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ public class Key implements Comparable<Key>, Serializable {
public static final Key moduleRecord = Key.of( "moduleRecord" );
public static final Key modulesDirectory = Key.of( "modulesDirectory" );
public static final Key month = Key.of( "month" );
public static final Key move = Key.of( "move" );
public static final Key multiCharacterDelimiter = Key.of( "multiCharacterDelimiter" );
public static final Key multipart = Key.of( "multipart" );
public static final Key multipartType = Key.of( "multipartType" );
Expand Down Expand Up @@ -402,6 +403,8 @@ public class Key implements Comparable<Key>, Serializable {
public static final Key queryTimeout = Key.of( "queryTimeout" );
public static final Key radix = Key.of( "radix" );
public static final Key Raw_Trace = Key.of( "Raw_Trace" );
public static final Key read = Key.of( "read" );
public static final Key readBinary = Key.of( "readBinary" );
public static final Key reapFrequency = Key.of( "reapFrequency" );
public static final Key recordCount = Key.of( "recordCount" );
public static final Key recurse = Key.of( "recurse" );
Expand Down Expand Up @@ -500,6 +503,8 @@ public class Key implements Comparable<Key>, Serializable {
public static final Key trim = Key.of( "trim" );
public static final Key type = Key.of( "type" );
public static final Key typename = Key.of( "typename" );
public static final Key upload = Key.of( "upload" );
public static final Key uploadAll = Key.of( "uploadAll" );
public static final Key URL = Key.of( "URL" );
public static final Key useCustomSerializer = Key.of( "useCustomSerializer" );
public static final Key useLastAccessTimeouts = Key.of( "useLastAccessTimeouts" );
Expand Down
Loading

0 comments on commit 147e0d6

Please sign in to comment.