2022-10-29 11:01:23 +00:00
|
|
|
---
|
|
|
|
module: [kind=event] file_transfer
|
2022-10-29 21:49:45 +00:00
|
|
|
since: 1.101.0
|
2022-10-29 11:01:23 +00:00
|
|
|
---
|
|
|
|
|
2023-03-15 21:52:13 +00:00
|
|
|
<!--
|
|
|
|
SPDX-FileCopyrightText: 2022 The CC: Tweaked Developers
|
|
|
|
|
|
|
|
SPDX-License-Identifier: MPL-2.0
|
|
|
|
-->
|
|
|
|
|
2023-08-24 09:48:30 +00:00
|
|
|
The [`file_transfer`] event is queued when a user drags-and-drops a file on an open computer.
|
2022-10-29 11:01:23 +00:00
|
|
|
|
2023-08-24 09:48:30 +00:00
|
|
|
This event contains a single argument of type [`TransferredFiles`], which can be used to [get the files to be
|
Always use raw bytes in file handles
Historically CC has supported two modes when working with file handles
(and HTTP requests):
- Text mode, which reads/write using UTF-8.
- Binary mode, which reads/writes the raw bytes.
However, this can be confusing at times. CC/Lua doesn't actually support
unicode, so any characters beyond the 0.255 range were replaced with
'?'. This meant that most of the time you were better off just using
binary mode.
This commit unifies text and binary mode - we now /always/ read the raw
bytes of the file, rather than converting to/from UTF-8. Binary mode now
only specifies whether handle.read() returns a number (and .write(123)
writes a byte rather than coercing to a string).
- Refactor the entire handle hierarchy. We now have an AbstractMount
base class, which has the concrete implementation of all methods. The
public-facing classes then re-export these methods by annotating
them with @LuaFunction.
These implementations are based on the
Binary{Readable,Writable}Handle classes. The Encoded{..}Handle
versions are now entirely removed.
- As we no longer need to use BufferedReader/BufferedWriter, we can
remove quite a lot of logic in Filesystem to handle wrapping
closeable objects.
- Add a new WritableMount.openFile method, which generalises
openForWrite/openForAppend to accept OpenOptions. This allows us to
support update mode (r+, w+) in fs.open.
- fs.open now uses the new handle types, and supports update (r+, w+)
mode.
- http.request now uses the new readable handle type. We no longer
encode the request body to UTF-8, nor decode the response from UTF-8.
- Websockets now return text frame's contents directly, rather than
converting it from UTF-8. Sending text frames now attempts to treat
the passed string as UTF-8, rather than treating it as latin1.
2023-11-08 19:37:10 +00:00
|
|
|
transferred][`TransferredFiles.getFiles`]. Each file returned is a [binary file handle][`fs.ReadHandle`] with an
|
2023-08-24 09:48:30 +00:00
|
|
|
additional [getName][`TransferredFile.getName`] method.
|
2022-10-29 11:01:23 +00:00
|
|
|
|
|
|
|
## Return values
|
2023-08-24 09:48:30 +00:00
|
|
|
1. [`string`]: The event name
|
|
|
|
2. [`TransferredFiles`]: The list of transferred files.
|
2022-10-29 11:01:23 +00:00
|
|
|
|
|
|
|
## Example
|
|
|
|
Waits for a user to drop files on top of the computer, then prints the list of files and the size of each file.
|
|
|
|
|
|
|
|
```lua
|
|
|
|
local _, files = os.pullEvent("file_transfer")
|
|
|
|
for _, file in ipairs(files.getFiles()) do
|
|
|
|
-- Seek to the end of the file to get its size, then go back to the beginning.
|
|
|
|
local size = file.seek("end")
|
|
|
|
file.seek("set", 0)
|
|
|
|
|
2023-10-03 08:19:19 +00:00
|
|
|
print(file.getName() .. " " .. size)
|
2022-10-29 11:01:23 +00:00
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
## Example
|
|
|
|
Save each transferred file to the computer's storage.
|
|
|
|
|
|
|
|
```lua
|
|
|
|
local _, files = os.pullEvent("file_transfer")
|
|
|
|
for _, file in ipairs(files.getFiles()) do
|
|
|
|
local handle = fs.open(file.getName(), "wb")
|
|
|
|
handle.write(file.readAll())
|
|
|
|
|
|
|
|
handle.close()
|
|
|
|
file.close()
|
|
|
|
end
|
|
|
|
```
|