1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
const std = @import("std");
const Storage = @import("root.zig");
const log = std.log.scoped(.user);
const Self = @This();
pub const Info = struct {
name: []const u8,
full_name: []const u8,
birthday: []const u8,
hash: []const u8,
is_admin: bool,
pub fn clone(self: *const @This(), allocator: std.mem.Allocator) !@This() {
var clone_self: @This() = undefined;
var i: usize = 0;
inline for (std.meta.fields(@This())) |field_info| {
if (field_info.type == []const u8) {
@field(clone_self, field_info.name) = try allocator.dupe(
u8,
@field(self, field_info.name),
);
errdefer allocator.free(@field(clone_self, field_info.name));
} else {
@field(clone_self, field_info.name) = @field(self, field_info.name);
}
i += 1;
}
return clone_self;
}
pub fn deinit(self: *const @This(), allocator: std.mem.Allocator) void {
inline for (std.meta.fields(@This())) |field_info| {
if (field_info.type == []const u8) {
allocator.free(@field(self, field_info.name));
}
}
}
};
dir: std.fs.Dir,
info: Info,
arena: std.heap.ArenaAllocator,
pub fn open(
storage: *Storage,
name: []const u8,
) !Self {
var arena: std.heap.ArenaAllocator = .init(storage.allocator);
errdefer arena.deinit();
const allocator = arena.allocator();
var user_dir = try storage.dir.openDir("user", .{});
defer user_dir.close();
var dir = try user_dir.openDir(name, .{});
errdefer dir.close();
const file = try dir.openFile("info.json", .{ .lock = .shared });
defer file.close();
const content = try file.readToEndAlloc(allocator, std.math.maxInt(usize));
const info = try std.json.parseFromSliceLeaky(Info, allocator, content, .{});
return .{
.dir = dir,
.arena = arena,
.info = info,
};
}
pub fn new(
storage: *Storage,
name: []const u8,
full_name: []const u8,
birthday: []const u8,
password: []const u8,
is_admin: bool,
inner_allocator: std.mem.Allocator
) !Self {
var arena: std.heap.ArenaAllocator = .init(inner_allocator);
const allocator = arena.allocator();
errdefer arena.deinit();
var user_dir = try storage.dir.openDir("user", .{});
defer user_dir.close();
try user_dir.makeDir(name);
var dir = try user_dir.openDir(name, .{});
errdefer dir.close();
var file = try dir.createFile("info.json", .{ .lock = .exclusive });
file.close();
const hash_buf = try allocator.alloc(u8, 256);
const hash = try std.crypto.pwhash.bcrypt.strHash(
password,
.{
.params = .{
.rounds_log = 3,
.silently_truncate_password = false,
},
.encoding = .phc,
},
hash_buf
);
const info: Info = .{
.name = name,
.full_name = full_name,
.birthday = birthday,
.hash = hash,
.is_admin = is_admin,
};
return .{
.dir = dir,
.arena = arena,
.info = info,
};
}
pub fn set_password(self: *Self, password: []const u8) !void {
const allocator = self.arena.allocator();
const hash_buf = try allocator.alloc(u8, 256);
const hash = try std.crypto.pwhash.bcrypt.strHash(
password,
.{
.params = .{
.rounds_log = 3,
.silently_truncate_password = false,
},
.encoding = .phc,
},
hash_buf
);
self.info.hash = hash;
}
pub fn sync(self: *Self) !void {
const file = try self.dir.createFile("info.json", .{
.lock = .exclusive,
});
defer file.close();
var buffer: [1024]u8 = undefined;
var writer = file.writer(&buffer);
var stringify = std.json.Stringify {
.writer = &writer.interface,
.options = .{ .whitespace = .indent_2 }
};
try stringify.write(self.info);
try writer.interface.flush();
}
pub fn check_password(self: *Self, password: []const u8) bool {
std.crypto.pwhash.bcrypt.strVerify(self.info.hash, password, .{
.silently_truncate_password = false,
}) catch return false;
return true;
}
pub fn deinit(self: *Self) void {
self.dir.close();
self.arena.deinit();
self.* = undefined;
}
pub fn exists(storage: *Storage, name: []const u8) bool {
var user = storage.dir.openDir("user", .{}) catch return false;
defer user.close();
user.access(name, .{}) catch return false;
return true;
}
pub fn image(storage: *Storage, name: []const u8) ?std.fs.File {
var user = storage.dir.openDir("user", .{}) catch return null;
defer user.close();
var profile = user.openDir(name, .{}) catch return null;
defer profile.close();
return profile.openFile("image.jpg", .{ .lock = .shared }) catch null;
}
pub fn set_image(
storage: *Storage,
name: []const u8,
reader: *std.Io.Reader,
size: usize,
) !void {
var user = try storage.dir.openDir("user", .{});
defer user.close();
var profile = try user.openDir(name, .{});
defer profile.close();
var image_file = try profile.createFile("image.jpg", .{ .lock = .exclusive });
defer image_file.close();
var buffer: [1024]u8 = undefined;
var file_writer = image_file.writer(&buffer);
log.info("uploaded to profile {s} [{} bytes]", .{ name, size });
try reader.streamExact(&file_writer.interface, size);
try file_writer.interface.flush();
}
pub fn list(
storage: *Storage,
allocator: std.mem.Allocator,
) ![][]const u8 {
var user = try storage.dir.openDir("user", .{ .iterate = true });
defer user.close();
var ids: std.ArrayList([]const u8) = .empty;
var iterator = user.iterate();
while (try iterator.next()) |entry| {
try ids.append(allocator, try allocator.dupe(u8, entry.name));
}
return ids.toOwnedSlice(allocator);
}
|