mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 23:24:09 +00:00
progress: reading binary data
This commit is contained in:
parent
0f6beb4347
commit
9517e7a253
2 changed files with 33 additions and 3 deletions
17
src/fio.zig
17
src/fio.zig
|
@ -136,6 +136,23 @@ pub extern fn fiobj_hash_haskey(hash: FIOBJ, key: FIOBJ) c_int;
|
|||
pub extern fn fiobj_ary_push(ary: FIOBJ, obj: FIOBJ) void;
|
||||
pub extern fn fiobj_float_new(num: f64) FIOBJ;
|
||||
pub extern fn fiobj_num_new_bignum(num: isize) FIOBJ;
|
||||
|
||||
pub extern fn fiobj_data_newstr() FIOBJ;
|
||||
pub extern fn fiobj_data_newstr2(buffer: ?*anyopaque, length: usize, dealloc: ?*const fn (?*anyopaque) callconv(.C) void) FIOBJ;
|
||||
pub extern fn fiobj_data_newtmpfile() FIOBJ;
|
||||
pub extern fn fiobj_data_newfd(fd: c_int) FIOBJ;
|
||||
pub extern fn fiobj_data_slice(parent: FIOBJ, offset: isize, length: usize) FIOBJ;
|
||||
pub extern fn fiobj_data_save(io: FIOBJ, filename: [*c]const u8) c_int;
|
||||
pub extern fn fiobj_data_read(io: FIOBJ, length: isize) fio_str_info_s;
|
||||
pub extern fn fiobj_data_read2ch(io: FIOBJ, token: u8) fio_str_info_s;
|
||||
pub extern fn fiobj_data_pos(io: FIOBJ) isize;
|
||||
pub extern fn fiobj_data_len(io: FIOBJ) isize;
|
||||
pub extern fn fiobj_data_seek(io: FIOBJ, position: isize) void;
|
||||
pub extern fn fiobj_data_pread(io: FIOBJ, start_at: isize, length: usize) fio_str_info_s;
|
||||
pub extern fn fiobj_data_write(io: FIOBJ, buffer: ?*anyopaque, length: usize) isize;
|
||||
pub extern fn fiobj_data_puts(io: FIOBJ, buffer: ?*anyopaque, length: usize) isize;
|
||||
pub extern fn fiobj_data_assert_dynamic(io: FIOBJ) void;
|
||||
|
||||
pub extern fn fiobj_free_wrapped(o: FIOBJ) callconv(.C) void;
|
||||
pub fn fiobj_null() callconv(.C) FIOBJ {
|
||||
return @as(FIOBJ, @bitCast(@as(c_long, FIOBJ_T_NULL)));
|
||||
|
|
19
src/zap.zig
19
src/zap.zig
|
@ -590,6 +590,7 @@ pub const HttpParamBinaryFile = struct {
|
|||
mimetype: ?[]const u8 = null,
|
||||
/// filename
|
||||
filename: ?[]const u8 = null,
|
||||
|
||||
pub fn format(value: @This(), comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) std.os.WriteError!void {
|
||||
const d = value.data orelse "\\0";
|
||||
const m = value.mimetype orelse "null";
|
||||
|
@ -611,15 +612,27 @@ pub fn Fiobj2HttpParam(o: fio.FIOBJ, a: std.mem.Allocator, dupe_string: bool) !?
|
|||
const key_name = fio.fiobj_str_new("name", 4);
|
||||
const key_data = fio.fiobj_str_new("data", 4);
|
||||
const key_type = fio.fiobj_str_new("type", 4);
|
||||
// files: they should have "data", "type", and "filename" keys
|
||||
defer {
|
||||
fio.fiobj_free_wrapped(key_name);
|
||||
fio.fiobj_free_wrapped(key_data);
|
||||
fio.fiobj_free_wrapped(key_type);
|
||||
} // files: they should have "data", "type", and "filename" keys
|
||||
if (fio.fiobj_hash_haskey(o, key_data) == 1 and fio.fiobj_hash_haskey(o, key_type) == 1 and fio.fiobj_hash_haskey(o, key_name) == 1) {
|
||||
// TODO: Fill the fields
|
||||
|
||||
const filename = fio.fiobj_obj2cstr(fio.fiobj_hash_get(o, key_name));
|
||||
const mimetype = fio.fiobj_obj2cstr(fio.fiobj_hash_get(o, key_type));
|
||||
const data = fio.fiobj_hash_get(o, key_data);
|
||||
|
||||
// the data
|
||||
const data_len = fio.fiobj_data_len(data);
|
||||
const data_buf = fio.fiobj_data_read(data, data_len);
|
||||
|
||||
if (data_buf.len != data_len) {
|
||||
std.log.warn("WARNING: HTTP param binary file size mismatch: should {d}, is: {d}", .{ data_len, data_buf.len });
|
||||
}
|
||||
return .{ .Unsupported_Hash = .{
|
||||
.filename = filename.data[0..filename.len],
|
||||
.mimetype = mimetype.data[0..mimetype.len],
|
||||
.data = data_buf.data[0..data_buf.len],
|
||||
} };
|
||||
} else {
|
||||
return .{ .Unsupported_Hash = .{} };
|
||||
|
|
Loading…
Add table
Reference in a new issue