| 1 | module main |
| 2 | |
| 3 | import time |
| 4 | import math |
| 5 | import os |
| 6 | |
| 7 | struct File { |
| 8 | id int @[primary; sql: serial] |
| 9 | repo_id int @[unique: 'file'] |
| 10 | name string @[unique: 'file'] |
| 11 | parent_path string @[unique: 'file'] |
| 12 | is_dir bool |
| 13 | branch string @[unique: 'file'] |
| 14 | contributors_count int |
| 15 | last_hash string |
| 16 | size int |
| 17 | is_size_calculated bool |
| 18 | views_count int |
| 19 | mut: |
| 20 | last_msg string |
| 21 | last_time int |
| 22 | commit Commit @[skip] |
| 23 | } |
| 24 | |
| 25 | fn (f File) url() string { |
| 26 | file_type := if f.is_dir { 'tree' } else { 'blob' } |
| 27 | |
| 28 | if f.parent_path == '' { |
| 29 | return '${file_type}/${f.branch}/${f.name}' |
| 30 | } |
| 31 | |
| 32 | return '${file_type}/${f.branch}/${f.parent_path}/${f.name}' |
| 33 | } |
| 34 | |
| 35 | fn (f &File) full_path() string { |
| 36 | if f.parent_path == '' { |
| 37 | return f.name |
| 38 | } |
| 39 | |
| 40 | return f.parent_path + '/' + f.name |
| 41 | } |
| 42 | |
| 43 | fn (f File) pretty_last_time() string { |
| 44 | if f.last_time == 0 { |
| 45 | return '' |
| 46 | } |
| 47 | return time.unix(f.last_time).relative() |
| 48 | } |
| 49 | |
| 50 | fn (f File) pretty_size() string { |
| 51 | if f.size == 0 { |
| 52 | return 'n/a' |
| 53 | } |
| 54 | |
| 55 | return pretty_size_bytes(f.size) |
| 56 | } |
| 57 | |
| 58 | fn (f File) pretty_tree_size() string { |
| 59 | if !f.is_dir || !f.is_size_calculated { |
| 60 | return '' |
| 61 | } |
| 62 | |
| 63 | return pretty_size_bytes(f.size) |
| 64 | } |
| 65 | |
| 66 | fn pretty_size_bytes(size_in_bytes int) string { |
| 67 | sizes := ['bytes', 'KB', 'MB', 'GB', 'TB'] |
| 68 | |
| 69 | if size_in_bytes == 0 { |
| 70 | return '0 bytes' |
| 71 | } |
| 72 | |
| 73 | index := int(math.floor(math.log(size_in_bytes) / math.log(1024))) |
| 74 | |
| 75 | if index == 0 { |
| 76 | return '${size_in_bytes} ${sizes[index]}' |
| 77 | } |
| 78 | |
| 79 | size_in := math.round_sig(size_in_bytes / (math.pow(1024, index)), 2) |
| 80 | |
| 81 | return '${size_in} ${sizes[index]}' |
| 82 | } |
| 83 | |
| 84 | struct FileInfo { |
| 85 | name string |
| 86 | last_msg string |
| 87 | last_hash string |
| 88 | last_time string |
| 89 | size string |
| 90 | } |
| 91 | |
| 92 | fn calculate_lines_of_code(source string) (int, int) { |
| 93 | lines := source.split_into_lines() |
| 94 | loc := lines.len |
| 95 | sloc := lines.filter(it.trim_space() != '').len |
| 96 | |
| 97 | return loc, sloc |
| 98 | } |
| 99 | |
| 100 | fn (mut app App) add_file(file File) ! { |
| 101 | sql app.db { |
| 102 | insert file into File |
| 103 | }! |
| 104 | } |
| 105 | |
| 106 | fn (mut app App) find_repository_items(repo_id int, branch string, parent_path string) []File { |
| 107 | valid_parent_path := if parent_path == '' { '.' } else { parent_path } |
| 108 | |
| 109 | items := sql app.db { |
| 110 | select from File where repo_id == repo_id && parent_path == valid_parent_path |
| 111 | && branch == branch |
| 112 | } or { []File{} } |
| 113 | |
| 114 | return items |
| 115 | } |
| 116 | |
| 117 | fn (mut app App) find_repo_file_by_path(repo_id int, item_branch string, path string) ?File { |
| 118 | mut valid_parent_path := os.dir(path) |
| 119 | item_name := path.after('/') |
| 120 | |
| 121 | if valid_parent_path == '' || valid_parent_path == '/' { |
| 122 | valid_parent_path = '.' |
| 123 | } |
| 124 | |
| 125 | app.info('find file repo_id=${repo_id} parent_path = ${valid_parent_path} branch=${item_branch} name=${item_branch}') |
| 126 | |
| 127 | files := sql app.db { |
| 128 | select from File where repo_id == repo_id && parent_path == valid_parent_path |
| 129 | && branch == item_branch && name == item_name limit 1 |
| 130 | } or { []File{} } |
| 131 | |
| 132 | if files.len == 0 { |
| 133 | return none |
| 134 | } |
| 135 | |
| 136 | return files.first() |
| 137 | } |
| 138 | |
| 139 | fn (mut app App) delete_repository_files(repository_id int) ! { |
| 140 | sql app.db { |
| 141 | delete from File where repo_id == repository_id |
| 142 | }! |
| 143 | } |
| 144 | |
| 145 | fn (mut app App) delete_repository_files_in_branch(repository_id int, branch_name string) ! { |
| 146 | sql app.db { |
| 147 | delete from File where repo_id == repository_id && branch == branch_name |
| 148 | }! |
| 149 | } |
| 150 | |
| 151 | fn (mut app App) delete_repo_folder(path string) { |
| 152 | if path == '' { |
| 153 | return |
| 154 | } |
| 155 | os.rmdir_all(os.real_path(path)) or { app.warn('failed to remove repo folder ${path}: ${err}') } |
| 156 | } |
| 157 | |