FileAPI: Difference between revisions

From FiguraMC
Lexize (talk | contribs)
Created page with "{{Host only|kind=API}} <code>FileAPI</code> allows your avatar to access files in isolated folder. FileAPI is available as global <code>file</code> variable. == Security == FileAPI is fully isolated in your data folder. It is located in your Figura folder, so, default path for it would be <code>figura/data</code>. FileAPI can't access any files or directories out of this folder, even when using symbolic links. Attempt to access file or directory outside data folder wi..."
 
m Follow style guide
 
(11 intermediate revisions by 3 users not shown)
Line 9: Line 9:
Every used path in methods is relative to data folder.
Every used path in methods is relative to data folder.


== FileAPI methods ==
== Methods ==
{| class="wikitable"
! Method
! Brief Description
|-
| [[#allowed|allowed]]
| Checks if FileAPI usage is allowed
|-
| [[#isPathAllowed|isPathAllowed]]
| Checks if a path is allowed for usage
|-
| [[#exists|exists]]
| Checks if a path exists
|-
| [[#delete|delete]]
| Deletes a file or directory, returning whether it was successful
|-
| [[#isDirectory|isDirectory]]
| Checks if a path is a directory
|-
| [[#mkdir|mkdir]]
| Creates a directory
|-
| [[#mkdirs|mkdirs]]
| Creates a directory and all parent directories
|-
| [[#list|list]]
| Lists files and directories contained under a path
|-
| [[#isFile|isFile]]
| Checks if a path is a file
|-
| [[#openReadStream|openReadStream]]
| Opens an input stream for a file
|-
| [[#openWriteStream|openWriteStream]]
| Opens an output stream for a file
|-
| [[#readString|readString]]
| Reads a file and returns its contents as a string
|-
| [[#writeString|writeString]]
| Writes a file's contents as a string
|}


=== Security checks ===
=== Security checks ===
Line 15: Line 58:
Security related methods of FileAPI.
Security related methods of FileAPI.


==== <code>FileAPI:allowed</code> ====
==== allowed ====
 
----
Checks if FileAPI usage is allowed for this avatar.
Checks if FileAPI usage is allowed for this avatar.


Overloads:
{| class="wikitable"
{| class="wikitable"
! Arguments !! Return Type
! Arguments !! Return Type
Line 31: Line 75:
</syntaxhighlight>
</syntaxhighlight>


==== <code>FileAPI:isPathAllowed</code> ====
==== isPathAllowed ====
 
----
Checks if path is allowed for usage.
Checks if path is allowed for usage.


Overloads:
{| class="wikitable"
{| class="wikitable"
! Arguments !! Return Type
! Arguments !! Return Type
Line 57: Line 102:
Methods related both to files and directories.
Methods related both to files and directories.


==== <code>FileAPI:exists</code> ====
==== exists ====
 
----
Checks if specified path exists.
Checks if specified path exists.


Overloads:
{| class="wikitable"
{| class="wikitable"
! Arguments !! Return Type
! Arguments !! Return Type
Line 69: Line 115:


<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
print(file:exists("foo"))
print(file:exists("foo.bar")) -- true
</syntaxhighlight>
</syntaxhighlight>


==== <code>FileAPI:delete</code> ====
==== delete ====
 
----
Deletes file or directory by specified path. Returns true if successful.
Deletes file or directory by specified path. Returns true if successful.


Overloads:
{| class="wikitable"
{| class="wikitable"
! Arguments !! Return Type
! Arguments !! Return Type
Line 84: Line 131:


<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- Directory foo is empty
-- Directory fooBar is empty
print(file:delete("foo")) -- true
print(file:delete("fooBar")) -- true
-- Directory bar has files inside
-- Directory barFoo has files inside
print(file:delete("bar")) -- false
print(file:delete("barFoo")) -- false
-- foo.bar is not open by any program
-- foo.bar is not open by any program
print(file:delete("foo.bar")) -- true
print(file:delete("foo.bar")) -- true
-- bar.foo is open by some program
-- bar.foo doesn't exist
print(file:delete("bar.foo")) -- false
print(file:delete("bar.foo")) -- false
</syntaxhighlight>
</syntaxhighlight>
Line 98: Line 145:
Directory related methods of FileAPI.
Directory related methods of FileAPI.


==== <code>FileAPI:isDirectory</code> ====
==== isDirectory ====
 
----
Checks if specified path is directory.
Checks if specified path is directory.


Overloads:
{| class="wikitable"
{| class="wikitable"
! Arguments !! Return Type
! Arguments !! Return Type
Line 110: Line 158:


<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
print(file:isDirectory("foo"))
print(file:isDirectory("fooBar")) -- true
</syntaxhighlight>
</syntaxhighlight>


==== <code>FileAPI:mkdir</code> ====
==== mkdir ====
 
----
Creates directory by specified path. Returns true if successful.
Creates directory by specified path. Returns true if successful.


Overloads:
{| class="wikitable"
{| class="wikitable"
! Arguments !! Return Type
! Arguments !! Return Type
Line 130: Line 179:
</syntaxhighlight>
</syntaxhighlight>


==== <code>FileAPI:mkdirs</code> ====
'''@see''' [[#exists|exists]]


==== mkdirs ====
----
Creates directory by specified path, and all parent directories if they doesn't exist. Returns true if successful.
Creates directory by specified path, and all parent directories if they doesn't exist. Returns true if successful.


Overloads:
{| class="wikitable"
{| class="wikitable"
! Arguments !! Return Type
! Arguments !! Return Type
Line 148: Line 200:
</syntaxhighlight>
</syntaxhighlight>


==== <code>FileAPI:list</code> ====
'''@see''' [[#exists|exists]], [[#mkdir|mkdir]]


==== list ====
----
Lists files and directories by specified path. Returns table with paths, or null if path doesn't exist or not a directory.
Lists files and directories by specified path. Returns table with paths, or null if path doesn't exist or not a directory.


Overloads:
{| class="wikitable"
{| class="wikitable"
! Arguments !! Return Type
! Arguments !! Return Type
|-
|-
| <code>list(path {{type|string}})</code>
| <code>list(path {{type|string}})</code>
| {{type|string}}[]?
| {{type|string[]?}}
|}
|}


<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local contents = file:list("path/to/dir")
printTable(file:list("")) --[[{
"foo.bar",
"fooBar",
"barFoo"
}]]
</syntaxhighlight>
</syntaxhighlight>


=== Files ===
=== Files ===
Methods related to files.
Methods related to files.


==== <code>FileAPI:openReadStream</code> ====
==== isFile ====
----
Checks if specified path is a file.


Overloads:
{| class="wikitable"
! Arguments !! Return Type
|-
| <code>isFile(path {{type|string}})</code>
| {{type|boolean}}
|}
<syntaxhighlight lang="lua">
print(file:isFile("foo.bar")) -- true
</syntaxhighlight>
==== openReadStream ====
----
Open input stream for file at specified path. Throws an error if file doesn't exist.
Open input stream for file at specified path. Throws an error if file doesn't exist.


Overloads:
{| class="wikitable"
{| class="wikitable"
! Arguments !! Return Type
! Arguments !! Return Type
Line 185: Line 262:
</syntaxhighlight>
</syntaxhighlight>


'''@see''' [[InputStream#close|InputStream:close]]


==== <code>FileAPI:openWriteStream</code> ====


==== openWriteStream ====
----
Open output stream for file at specified path. Throws an error if unable to create a file.
Open output stream for file at specified path. Throws an error if unable to create a file.


Overloads:
{| class="wikitable"
{| class="wikitable"
! Arguments !! Return Type
! Arguments !! Return Type
Line 205: Line 285:
</syntaxhighlight>
</syntaxhighlight>


==== <code>FileAPI:readString</code> ====


Reads file at specified path and returns it's contents as a string.
'''@see''' [[OutputStream#close|OutputStream:close]]
 
==== readString ====
----
Reads file at specified path and returns it's contents as a string in specified encoding.
Default encoding is <code>utf8</code>.


Overloads:
{| class="wikitable"
{| class="wikitable"
! Arguments !! Return Type
! Arguments !! Return Type
Line 215: Line 300:
| {{type|string}}
| {{type|string}}
|-
|-
| <code>readString(path {{type|string}}, encoding {{type|string}})</code>
| <code>readString(path {{type|string}}, encoding {{type|BufferEncoding}})</code>
| {{type|string}}
| {{type|string}}
|}
|}
Line 223: Line 308:
</syntaxhighlight>
</syntaxhighlight>


==== <code>FileAPI:writeString</code> ====
==== writeString ====
 
----
Writes string in file by specified path.
Writes string in specified encoding to file by specified path.
Default encoding is <code>utf8</code>.


Overloads:
{| class="wikitable"
{| class="wikitable"
! Arguments
! Arguments
Line 232: Line 319:
| <code>writeString(path {{type|string}}, contents {{type|string}})</code>
| <code>writeString(path {{type|string}}, contents {{type|string}})</code>
|-
|-
| <code>writeString(path {{type|string}}, contents {{type|string}}, encoding {{type|string}})</code>
| <code>writeString(path {{type|string}}, contents {{type|string}}, encoding {{type|BufferEncoding}})</code>
|}
|}


Line 238: Line 325:
print(file:writeString("foo.bar", "Hello, world!"))
print(file:writeString("foo.bar", "Hello, world!"))
</syntaxhighlight>
</syntaxhighlight>
== Navigation ==
{{Navbox documentation}}

Latest revision as of 19:55, 5 October 2024

This API is only available on the host.

For more information, see Pings.

FileAPI allows your avatar to access files in isolated folder.

FileAPI is available as global file variable.

Security

FileAPI is fully isolated in your data folder. It is located in your Figura folder, so, default path for it would be figura/data. FileAPI can't access any files or directories out of this folder, even when using symbolic links. Attempt to access file or directory outside data folder will result in error. Every used path in methods is relative to data folder.

Methods

Method Brief Description
allowed Checks if FileAPI usage is allowed
isPathAllowed Checks if a path is allowed for usage
exists Checks if a path exists
delete Deletes a file or directory, returning whether it was successful
isDirectory Checks if a path is a directory
mkdir Creates a directory
mkdirs Creates a directory and all parent directories
list Lists files and directories contained under a path
isFile Checks if a path is a file
openReadStream Opens an input stream for a file
openWriteStream Opens an output stream for a file
readString Reads a file and returns its contents as a string
writeString Writes a file's contents as a string

Security checks

Security related methods of FileAPI.

allowed


Checks if FileAPI usage is allowed for this avatar.

Overloads:

Arguments Return Type
allowed() boolean
-- Prints false on non-host avatar.
print(file:allowed())

isPathAllowed


Checks if path is allowed for usage.

Overloads:

Arguments Return Type
isPathAllowed(path string) boolean
-- Relative paths are always relative to data folder.
print(file:isPathAllowed("foo")) -- true
-- Relative paths that are pointing outside of data folder are not allowed.
print(file:isPathAllowed("../foo")) -- false
-- Absolute paths pointing to figura data folder are also allowed.
print(file:isPathAllowed("C:/path/to/figura/data/foo")) -- true
-- Absolute paths outside of data folder are not allowed.
print(file:isPathAllowed("C:/foo")) -- false

Common

Methods related both to files and directories.

exists


Checks if specified path exists.

Overloads:

Arguments Return Type
exists(path string) boolean
print(file:exists("foo.bar")) -- true

delete


Deletes file or directory by specified path. Returns true if successful.

Overloads:

Arguments Return Type
delete(path string) boolean
-- Directory fooBar is empty
print(file:delete("fooBar")) -- true
-- Directory barFoo has files inside
print(file:delete("barFoo")) -- false
-- foo.bar is not open by any program
print(file:delete("foo.bar")) -- true
-- bar.foo doesn't exist
print(file:delete("bar.foo")) -- false

Directories

Directory related methods of FileAPI.

isDirectory


Checks if specified path is directory.

Overloads:

Arguments Return Type
isDirectory(path string) boolean
print(file:isDirectory("fooBar")) -- true

mkdir


Creates directory by specified path. Returns true if successful.

Overloads:

Arguments Return Type
mkdir(path string) boolean
print(file:exists("foo")) -- false
print(file:mkdir("foo")) -- true
print(file:exists("foo")) -- true

@see exists

mkdirs


Creates directory by specified path, and all parent directories if they doesn't exist. Returns true if successful.

Overloads:

Arguments Return Type
mkdir(path string) boolean
print(file:exists("foo/bar")) -- false
print(file:mkdir("foo/bar")) -- false
print(file:mkdirs("foo/bar")) -- true
print(file:exists("foo/bar")) -- true

@see exists, mkdir

list


Lists files and directories by specified path. Returns table with paths, or null if path doesn't exist or not a directory.

Overloads:

Arguments Return Type
list(path string) string[] | nil
printTable(file:list("")) --[[{
"foo.bar",
"fooBar",
"barFoo"
}]]


Files

Methods related to files.

isFile


Checks if specified path is a file.

Overloads:

Arguments Return Type
isFile(path string) boolean
print(file:isFile("foo.bar")) -- true

openReadStream


Open input stream for file at specified path. Throws an error if file doesn't exist.

Overloads:

Arguments Return Type
openReadStream(path string) InputStream
-- Opens an input stream.
local is = file:openReadStream("foo.bar")

-- Stream must be closed when you finished working with it.
is:close()

@see InputStream:close


openWriteStream


Open output stream for file at specified path. Throws an error if unable to create a file.

Overloads:

Arguments Return Type
openWriteStream(path string) OutputStream
-- Opens an output stream.
local os = file:openWriteStream("bar.foo")

-- Stream must be closed when you finished working with it.
os:close()


@see OutputStream:close

readString


Reads file at specified path and returns it's contents as a string in specified encoding. Default encoding is utf8.

Overloads:

Arguments Return Type
readString(path string) string
readString(path string, encoding BufferEncoding) string
print(file:readString("foo.bar")) -- Hello, world!

writeString


Writes string in specified encoding to file by specified path. Default encoding is utf8.

Overloads:

Arguments
writeString(path string, contents string)
writeString(path string, contents string, encoding BufferEncoding)
print(file:writeString("foo.bar", "Hello, world!"))

Navigation

Documentation
Globals
APIs (Types)
Visuals (Types)
Other Types
Enums