IO helper slots for Magic to manipulate files and folders on your server using Hyperlambda. To use package go to https://polterguy.github.io
$ dotnet add package magic.lambda.ioThis project provides file/folder slots for Magic. More specifically, it provides the following slots.
magic.lambda.io can only manipulate files and folders inside of your "files" folder in your web server. This are normally files inside of the folder you have configured in your "appsettings.json" file, with the key "magic.io.root-folder". This implies that all paths are relative to this path, and no files or folders from outside of this folder can in any ways be manipulated using these slots. These are the files you can see when you open Hyper IDE.
Notice, if you wish to change this configuration value, then the character tilde "~" means root folder where your application is running from within. There is nothing preventing you from using an absolute path, but if you do, you must make sure your web server process have full rights to modify files within this folder. We do not recommend changing this configuration setting.
Creates a new folder on disc. The example below will create a folder named "foo" inside of the "misc" folder. Notice, will throw an exception if the folder already exists.
io.folder.create:/misc/foo/
Returns true if specified folder exists on disc.
io.folder.exists:/misc/foo/
Deletes the specified folder on disc. Notice, will throw an exception if the folder doesn't exists.
io.folder.delete:/misc/foo/
Lists all folders inside of the specified folder. By default hidden folders will not be shown, unless you pass in [display-hidden] and set its value to boolean "true".
io.folder.list:/misc/
Moves the specified source folder to its specified destination folder.
io.folder.move:/misc/source-folder/
.:/misc/destination-folder/
Copies the specified source folder to its specified destination folder. Notice, this slot doesn't copy the folder itself, but rather the folder's content. Below is an example.
io.folder.copy:/misc/source-folder/
.:/misc/destination-folder/
After invocation of the above, every single file and folder inside your "source-folder" can now be found as a copy inside your "destination-folder".
Loads the specified text file from disc. This slot can only load text files. Or to be specific, there are no ways you can change binary files, hence loading a binary file is for the most parts not something you should do. Although there might exist exceptions to this.
io.file.load:/misc/README.md
Notice - If you want to load binary content you should use the [io.file.load.binary] override.
Saves the specified content to the specified file on disc, overwriting any previous content if the file exists from before, creating a new file if no such file already exists. The value of the first argument will be considered the content of the file.
Notice, the node itself will be evaluated, allowing you to have other slots evaluated before slot saves the file, to return dynamically the content of your file.
io.file.save:/misc/README2.md
.:This is new content for file
Notice - If you want to save binary content you should use the [io.file.save.binary] override.
Returns true if specified file exists from before.
io.file.exists:/misc/README.md
Deletes the specified file. Will throw an exception if the file doesn't exist.
io.file.load:/misc/DOES-NOT-EXIST.md
Copies the specified file to the specified destination folder and file. Notice, requires the destination folder to exist from before, and the source file to exist from before. This slot will delete any previously existing destination file, before starting the copying process. Just like the save slot, this will evaluate the lambda children before it executes the copying of your file, allowing you to use the results of slots as the destination path for your file.
io.file.copy:/misc/README.md
.:/misc/backup/README-backup.md
Notice, the folder parts of thye destination folder is optional, and if you don't supply a folder as a part of the path, the source folder will be used by default.
Executes the specified Hyperlambda file. Just like when evaluating a dynamic slot, you can pass in an [.arguments] node to the file, which will be considered arguments to your file. Hence, this slot allows you to invoke a file, as if it was a dynamically created slot, and there is no semantic difference really between this slot and [signal] from the magic.lambda.slots project.
io.file.execute:/misc/some-hyperlambda-file.hl
The [execute-file] slot is an alias for [io.file.execute], but it will [unwrap] all children nodes, automatically transform any expressions to their static value equivalents, allowing you to combine [unwrap] and [io.file.execute] into one invocation. This makes usage slightly less verbose for cases such as the following.
.args
foo1:bar1
foo2:bar2
execute-file:/whatever-file.hl
arg1:x:@.args/*/foo1
arg2:x:@.args/*/foo2
In the above example both arguments to our "/whatever-file.hl" file will be evaluated before the file is executed, resulting in [arg1] and [args2] having the values of "bar1" and "bar2" respectively.
If you pass in a node named [node_reference] using the above syntax, this node will be especially handled, and passed in by reference instead of evaluated as a single expression value. Consider the following alternative to the above.
.args
foo1:bar1
foo2:bar2
execute-file:/whatever-file.hl
node_reference:x:@.args
Inside your above "/whatever-file.hl" file you will now have access to the entire [.args] node by reference.
Lists all files inside of the specified folder. By default hidden files will not be shown, unless you pass in [display-hidden] and set its value to boolean "true".
io.file.list:/misc/
Similar to [io.file.copy] but deletes the source file after evaluating.
io.file.move:/misc/README.md
.:/misc/backup/README-backup.md
Unzips a ZIP file. Notice, the [folder] argument is optional, and the current folder of the ZIP file you're unzipping will be used if no [folder] argument is given.
io.file.unzip:/misc/foo.zip
folder:/misc/backup/
This slot takes a filename as its primary argument, and optionally any amount of lambda children objects.
It allows for dynamically substituting for instance {{ '{{' }}*/.name}} segments in your original source file,
by invoking lambda objects it can find by evaluating your {{ '{{' }}xyz}} segment as an expression
leading to some lambda object you want to execute. Below is an example of usage that assumes you've got a
file named "foo.html" at the root folder of your installation resembling the following.
/foo.html
<html>
<head>
<title>Hello world</title>
</head>
<body>
<h1>Hello world</h1>
<p>
Hello there {{ '{{' }}*/.name}}, 2 + 5 is {{ '{{' }}*/.add}}
</p>
</body>
</html>
If you create the above file you can invoke [io.file.mixin] as follows to see the result.
io.file.mixin:/foo.html
.name:Thomas Hansen
.add
math.add
.:int:2
.:int:5
return:x:-
The above will substitute all your {{xyz}} segments and give you a result resembling the following.
<html>
<head>
<title>Hell world</title>
</head>
<body>
<h1>Hello world</h1>
<p>
Hello there Thomas Hansen, 2 + 5 is 7
</p>
</body>
</html>
Notice, if the node your expression is leading to has a value such as illustrated above, it will return that value instead of executing your node as a lambda object.
All [.oninit] lambda objects found as children of your [io.file.mixin] invocation will be executed before any mixin logic is executed, allowing for you to create common initialisation logic, that runs once but is referenced in multiple mixin expressions.
Creates a memory based ZIP stream you can return over the HTTP response object. Notice, this doesn't create a zip file, but rather a zip stream, which you can manipulate using other slots. This slot is useful if you need to return zipped content as your HTTP response for instance.
Notice, both the root arguments (lambda children) of this slot will be evaluated, in addition to its content nodes, evaluated once for each file declaration node. Notice also that the stream is a memory bases stream, and hence closing it, even in case of an exception, is not necessary.
io.content.zip-stream
.:/foo/x.txt
.:content of file
Works similarly to [io.file.load] but instead of returning the file's content, it returns the raw stream back to caller.
io.stream.open-file:/foo/bar.txt
After invoking the above, assuming the file exists, a raw Stream object can be
found as the value of the [io.stream.open-file] node.
Works similarly to [io.file.save] but instead of taking source content of some kind,
it assumes the source is an open Stream of some sort.
/*
* [.stream] here is an open stream, from for instance the HTTP
* request object, or something similar that somehow is able to open
* a stream and pass around in Hyperlambda.
*/
.stream
io.stream.save-file:/foo/bar.txt
get-value:x:@.stream
After invoking the above, assuming the [.stream] node contains a valid Stream
object, the file above will contain the content from the stream.
Notice - If the file exists from before the existing file will be deleted, unless you pass in an [overwrite] argument and set its value to false, at which point an exception will be thrown if the file exists from before.
Works similarly to [io.file.load] but instead of taking a source filename of some kind,
it assumes the source is an open Stream of some sort.
/*
* [.stream] here is an open stream, from for instance the HTTP
* request object, or something similar.
*/
.stream
io.stream.read:x:@.stream
Closes a previously opened stream.
Notice - You would rarely directly use streams from Hyperlambda, and not manipulate them in any ways, but rather use for instance [io.file.load] and similar "high level" slots - And only use streams when you need to directly access the HTTP request stream, to persist a file uploaded by a user, or return a file over the HTTP response object. Hence, directly opening a stream for any other purpose but to return it over the HTTP response object is something you'd probably never want to do. And if you return the stream over the HTTP response object, .Net takes ownership over the stream, and ensures it is closed and disposed. However, for completeness we've still provided the ability to explicitly close a stream using the [io.stream.close] - Even though you would probably never really need to use it. Besides, opening streams for any other purpose but to return them over the HTTP response object, might also create leaks, since there is no means to guarantee that the stream is close in case of exceptions, etc - Unless you explicitly take care of such things manually.
io.stream.open-file:/foo/bar.txt
io.stream.close:x:-
After invoking the above, assuming [.stream] is a valid stream, the stream's raw
byte[] content can be found in [io.stream.read].
Returns the root folder of the system. Cannot be invoked from Hyperlambda, but only from C#. Intended as a support function for other C# slots.
var node = new Node();
signaler.Signal(".io.folder.root", node);
// Retrieving root folder after evaluating slot.
var rootFolder = node.Get<string>();
Magic is 100% Open Source and you can find the primary project GitHub page here.
The source code for this repository can be found at github.com/polterguy/magic.lambda.io, and you can provide feedback, provide bug reports, etc at the same place.
The projects is copyright Thomas Hansen 2023 - 2024, and professionally maintained by AINIRO.IO.