-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathavatar.v
68 lines (48 loc) · 1.67 KB
/
avatar.v
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
module main
import os
const default_avatar_name = 'default_avatar.png'
const assets_path = 'assets'
const avatar_max_file_size = 1 * 1024 * 1024 // 1 megabyte
const supported_mime_types = [
'image/jpeg',
'image/png',
'image/webp',
]
fn validate_avatar_content_type(content_type string) bool {
return supported_mime_types.contains(content_type)
}
fn extract_file_extension_from_mime_type(mime_type string) !string {
is_valid_mime_type := validate_avatar_content_type(mime_type)
if !is_valid_mime_type {
return error('MIME type is not supported')
}
mime_parts := mime_type.split('/')
return mime_parts[1]
}
fn validate_avatar_file_size(content string) bool {
return content.len <= avatar_max_file_size
}
fn (app App) build_avatar_file_path(avatar_filename string) string {
relative_path := os.join_path(app.config.avatars_path, avatar_filename)
return os.abs_path(relative_path)
}
fn (app App) build_avatar_file_url(avatar_filename string) string {
clean_path := app.config.avatars_path.trim_string_left('./')
return os.join_path('/', clean_path, avatar_filename)
}
fn (app App) write_user_avatar(avatar_filename string, file_content string) bool {
path := os.join_path(app.config.avatars_path, avatar_filename)
os.write_file(path, file_content) or { return false }
return true
}
fn (app App) prepare_user_avatar_url(avatar_filename_or_url string) string {
is_url := avatar_filename_or_url.starts_with('http')
is_default_avatar := avatar_filename_or_url == default_avatar_name
if is_url {
return avatar_filename_or_url
}
if is_default_avatar {
return os.join_path('/', assets_path, avatar_filename_or_url)
}
return app.build_avatar_file_url(avatar_filename_or_url)
}