Packages
mist
4.0.4
6.0.3
6.0.2
6.0.1
6.0.0
5.0.4
5.0.3
5.0.2
5.0.1
5.0.0
5.0.0-rc1
4.0.7
4.0.6
4.0.5
4.0.4
4.0.3
4.0.2
4.0.1
4.0.0
3.0.0
2.0.0
1.2.0
1.1.0
1.0.0
1.0.0-rc3
1.0.0-rc2
1.0.0-rc1
0.17.0
0.15.0
0.14.3
0.14.2
0.14.1
0.14.0
0.13.2
0.13.1
0.13.0
0.12.0
0.11.0
0.10.0
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.3
0.8.2
0.8.1
0.8.0
0.7.1
0.7.0
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.1
0.2.0
0.1.3
a misty Gleam web server
Current section
Files
Jump to
Current section
Files
src/mist/internal/file.gleam
import gleam/bytes_tree
import gleam/result
import glisten.{type Socket, type SocketReason}
import glisten/transport.{type Transport, Ssl, Tcp}
pub type FileDescriptor
pub type FileError {
IsDir
NoAccess
NoEntry
UnknownFileError
}
pub type SendError {
FileErr(FileError)
SocketErr(SocketReason)
}
pub type File {
File(descriptor: FileDescriptor, file_size: Int)
}
pub fn stat(filename: BitArray) -> Result(File, FileError) {
filename
|> open
|> result.map(fn(fd) {
let file_size = size(filename)
File(fd, file_size)
})
}
pub fn sendfile(
transport: Transport,
file_descriptor file_descriptor: FileDescriptor,
socket socket: Socket,
offset offset: Int,
bytes bytes: Int,
options options: List(a),
) -> Result(Nil, SendError) {
case transport {
Tcp(..) -> {
send_file(file_descriptor, socket, offset, bytes, options)
|> result.map_error(SocketErr)
|> result.replace(Nil)
}
Ssl(..) as transport -> {
pread(file_descriptor, offset, bytes)
|> result.map_error(FileErr)
|> result.then(fn(bits) {
transport.send(transport, socket, bytes_tree.from_bit_array(bits))
|> result.map_error(SocketErr)
})
}
}
}
@external(erlang, "file", "sendfile")
fn send_file(
file_descriptor file_descriptor: FileDescriptor,
socket socket: Socket,
offset offset: Int,
bytes bytes: Int,
options options: List(a),
) -> Result(Nil, SocketReason)
@external(erlang, "file", "pread")
fn pread(
fd: FileDescriptor,
location: Int,
bytes: Int,
) -> Result(BitArray, FileError)
@external(erlang, "mist_ffi", "file_open")
pub fn open(file: BitArray) -> Result(FileDescriptor, FileError)
@external(erlang, "filelib", "file_size")
fn size(path: BitArray) -> Int
@external(erlang, "mist_ffi", "file_close")
pub fn close(file: FileDescriptor) -> Result(Nil, FileError)