poolList.html 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  1. <style>
  2. #spRoot {
  3. font-family: var(--ss-font, -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif);
  4. color: var(--ss-text);
  5. }
  6. #spActions { display: flex; align-items: center; gap: 8px; }
  7. /* Drive rows */
  8. #spRoot #spDriveSections { margin-bottom: 18px; }
  9. #spRoot #spDriveSections .ss-card { overflow: visible; } /* The drive menu reaches past the card */
  10. #spRoot .sp-drive {
  11. display: flex;
  12. align-items: center;
  13. gap: 12px;
  14. padding: 10px 14px;
  15. border-bottom: 1px solid var(--ss-border);
  16. }
  17. #spRoot .sp-drive:last-child { border-bottom: none; }
  18. #spRoot .sp-drive-icon { width: 30px; height: 30px; flex-shrink: 0; }
  19. #spRoot .sp-drive-main { flex: 1; min-width: 0; }
  20. #spRoot .sp-drive-title {
  21. display: flex;
  22. align-items: center;
  23. flex-wrap: wrap;
  24. gap: 2px 8px;
  25. font-size: 13px;
  26. font-weight: 600;
  27. }
  28. #spRoot .sp-id {
  29. font-family: var(--ss-mono);
  30. font-size: 11.5px;
  31. font-weight: 400;
  32. color: var(--ss-text-2);
  33. }
  34. #spRoot .sp-drive-who {
  35. margin-top: 2px;
  36. font-size: 12px;
  37. color: var(--ss-text-2);
  38. overflow-wrap: anywhere;
  39. }
  40. #spRoot .sp-drive-actions { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
  41. /* A drive that is not serving files. Its actions stay as they are */
  42. #spRoot .sp-drive.inactive .sp-drive-icon { filter: grayscale(1); opacity: 0.55; }
  43. #spRoot .sp-drive.inactive .sp-drive-title,
  44. #spRoot .sp-drive.inactive .sp-id,
  45. #spRoot .sp-drive.inactive .sp-drive-who { color: var(--ss-muted); }
  46. #spRoot .sp-drive.inactive .ss-badge { background: var(--ss-surface-3); color: var(--ss-text-2); }
  47. #spRoot .sp-fixed { font-size: 12px; color: var(--ss-muted); }
  48. #spRoot .sp-link {
  49. padding: 0;
  50. border: none;
  51. background: none;
  52. font: inherit;
  53. color: var(--ss-link);
  54. cursor: pointer;
  55. }
  56. #spRoot .sp-link:hover { text-decoration: underline; }
  57. /* The menu of a drive */
  58. #spRoot .sp-menu-wrap { position: relative; }
  59. #spRoot .sp-menu {
  60. position: absolute;
  61. right: 0;
  62. top: calc(100% + 4px);
  63. z-index: 20;
  64. display: none;
  65. min-width: 130px;
  66. padding: 4px;
  67. background: var(--ss-surface);
  68. border: 1px solid var(--ss-border-strong);
  69. border-radius: var(--ss-radius-sm);
  70. box-shadow: var(--ss-shadow-pop);
  71. }
  72. #spRoot .sp-menu.open { display: block; }
  73. #spRoot .sp-menu button {
  74. display: block;
  75. width: 100%;
  76. padding: 6px 10px;
  77. border: none;
  78. border-radius: 5px;
  79. background: none;
  80. font: inherit;
  81. font-size: 12.5px;
  82. text-align: left;
  83. color: var(--ss-text);
  84. cursor: pointer;
  85. }
  86. #spRoot .sp-menu button:hover { background: var(--ss-hover); }
  87. #spRoot .sp-menu button.danger { color: var(--ss-danger); }
  88. /* Who can access */
  89. #spAccessView { container-type: inline-size; }
  90. #spRoot .sp-matrix-wrap { overflow-x: auto; }
  91. #spRoot .sp-matrix { width: 100%; border-collapse: collapse; font-size: 12.5px; }
  92. #spRoot .sp-matrix th,
  93. #spRoot .sp-matrix td {
  94. padding: 8px 10px;
  95. border-bottom: 1px solid var(--ss-border);
  96. text-align: left;
  97. white-space: nowrap;
  98. }
  99. #spRoot .sp-matrix tr:last-child th,
  100. #spRoot .sp-matrix tr:last-child td { border-bottom: none; }
  101. #spRoot .sp-matrix thead th { font-weight: 600; color: var(--ss-text-2); }
  102. #spRoot .sp-matrix tbody th {
  103. position: sticky;
  104. left: 0;
  105. z-index: 1;
  106. background: var(--ss-surface);
  107. font-weight: 600;
  108. }
  109. #spRoot .sp-matrix .sp-id { display: block; }
  110. #spRoot .sp-matrix tr.inactive th,
  111. #spRoot .sp-matrix tr.inactive .sp-id,
  112. #spRoot .sp-rule-row.inactive > span:first-child,
  113. #spRoot .sp-rule-row.inactive .sp-id { color: var(--ss-muted); }
  114. #spRoot .sp-level { min-width: 118px; }
  115. #spRoot .sp-level.inherit { color: var(--ss-muted); }
  116. #spRoot .sp-locked { color: var(--ss-text-2); }
  117. #spRoot .sp-locked small { display: block; font-size: 11px; color: var(--ss-muted); }
  118. #spRoot .sp-narrow { display: none; }
  119. @container (max-width: 640px) {
  120. #spRoot .sp-wide { display: none; }
  121. #spRoot .sp-narrow { display: block; }
  122. }
  123. /* Check a user */
  124. #spRoot .sp-check { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 10px 14px; }
  125. #spRoot .sp-check .ss-input { width: 170px; }
  126. #spRoot .sp-check-result { padding: 0 14px 10px; font-size: 12.5px; color: var(--ss-text-2); }
  127. #spRoot .sp-check-result:empty { display: none; }
  128. /* The panel for adding, editing and the access list of one drive */
  129. #spPanelBack {
  130. position: fixed;
  131. inset: 0;
  132. z-index: 900;
  133. display: none;
  134. background: rgba(0, 0, 0, 0.32);
  135. }
  136. #spPanelBack.open { display: block; }
  137. #spPanel {
  138. position: absolute;
  139. top: 0;
  140. right: 0;
  141. bottom: 0;
  142. display: flex;
  143. flex-direction: column;
  144. width: min(500px, 100%);
  145. background: var(--ss-bg);
  146. box-shadow: var(--ss-shadow-pop);
  147. }
  148. #spRoot .sp-panel-head {
  149. display: flex;
  150. align-items: flex-start;
  151. justify-content: space-between;
  152. gap: 12px;
  153. padding: 16px 18px 12px;
  154. border-bottom: 1px solid var(--ss-border);
  155. }
  156. #spRoot .sp-panel-title { font-size: 15px; font-weight: 600; }
  157. #spRoot .sp-panel-sub { margin-top: 2px; font-size: 12px; color: var(--ss-text-2); }
  158. #spRoot .sp-panel-body { flex: 1; min-height: 0; padding: 14px 18px; overflow-y: auto; }
  159. #spRoot .sp-panel-foot {
  160. display: flex;
  161. align-items: center;
  162. justify-content: flex-end;
  163. gap: 8px;
  164. padding: 12px 18px;
  165. border-top: 1px solid var(--ss-border);
  166. }
  167. #spRoot .sp-panel-foot .sp-grow { flex: 1; }
  168. #spRoot .sp-close {
  169. width: 26px;
  170. height: 26px;
  171. padding: 0;
  172. border: none;
  173. border-radius: 6px;
  174. background: none;
  175. font-size: 17px;
  176. line-height: 1;
  177. color: var(--ss-text-2);
  178. cursor: pointer;
  179. }
  180. #spRoot .sp-close:hover { background: var(--ss-hover); }
  181. /* Step one */
  182. #spRoot .sp-choice {
  183. display: flex;
  184. align-items: center;
  185. gap: 12px;
  186. width: 100%;
  187. margin-bottom: 8px;
  188. padding: 12px 14px;
  189. border: 1px solid var(--ss-border-strong);
  190. border-radius: var(--ss-radius);
  191. background: var(--ss-surface);
  192. font: inherit;
  193. text-align: left;
  194. color: var(--ss-text);
  195. cursor: pointer;
  196. }
  197. #spRoot .sp-choice:hover { border-color: var(--ss-accent); }
  198. #spRoot .sp-choice img { width: 30px; height: 30px; flex-shrink: 0; }
  199. #spRoot .sp-choice b { display: block; font-size: 13px; font-weight: 600; }
  200. #spRoot .sp-choice span { display: block; margin-top: 1px; font-size: 12px; color: var(--ss-text-2); }
  201. /* Form fields */
  202. #spRoot .sp-field { margin-bottom: 12px; }
  203. #spRoot .sp-field label,
  204. #spRoot .sp-field-label { display: block; margin-bottom: 4px; font-size: 12.5px; font-weight: 600; }
  205. #spRoot .sp-field .ss-input,
  206. #spRoot .sp-field .ss-select { width: 100%; }
  207. #spRoot .sp-hint { margin-top: 3px; font-size: 11.5px; color: var(--ss-text-2); }
  208. #spRoot .sp-error { margin-top: 3px; font-size: 11.5px; color: var(--ss-danger); }
  209. #spRoot .sp-error:empty { display: none; }
  210. #spRoot .sp-inline { display: flex; gap: 6px; }
  211. #spRoot .sp-inline .ss-input { flex: 1; min-width: 0; }
  212. #spRoot .sp-switch-row {
  213. display: flex;
  214. align-items: center;
  215. justify-content: space-between;
  216. gap: 14px;
  217. margin-bottom: 12px;
  218. }
  219. #spRoot .sp-switch-row b { display: block; font-size: 12.5px; font-weight: 600; }
  220. #spRoot .sp-switch-row span.sp-hint { display: block; }
  221. #spRoot .sp-rule-row {
  222. display: flex;
  223. align-items: center;
  224. justify-content: space-between;
  225. gap: 12px;
  226. padding: 8px 12px;
  227. border-bottom: 1px solid var(--ss-border);
  228. font-size: 12.5px;
  229. }
  230. #spRoot .sp-rule-row:last-child { border-bottom: none; }
  231. #spRoot .sp-rule-row > span:first-child { font-weight: 600; overflow-wrap: anywhere; }
  232. #spRoot .sp-note-block { margin-bottom: 12px; }
  233. #spRoot .sp-panel-error {
  234. margin-bottom: 12px;
  235. padding: 8px 10px;
  236. border-radius: var(--ss-radius-sm);
  237. background: var(--ss-danger-soft);
  238. font-size: 12px;
  239. color: var(--ss-danger);
  240. }
  241. #spRoot .sp-panel-error:empty { display: none; }
  242. #spRoot .sp-test-result { font-size: 12px; color: var(--ss-text-2); }
  243. #spRoot .sp-test-result.ok { color: var(--ss-success); }
  244. #spRoot .sp-test-result.bad { color: var(--ss-danger); }
  245. #spRoot .sp-partition {
  246. display: block;
  247. width: 100%;
  248. margin-top: 4px;
  249. padding: 7px 10px;
  250. border: 1px solid var(--ss-border);
  251. border-radius: var(--ss-radius-sm);
  252. background: var(--ss-surface);
  253. font: inherit;
  254. font-size: 12px;
  255. text-align: left;
  256. color: var(--ss-text);
  257. cursor: pointer;
  258. }
  259. #spRoot .sp-partition:hover { border-color: var(--ss-accent); }
  260. #spRoot .sp-disk-name { margin-top: 8px; font-size: 12px; font-weight: 600; }
  261. #spRoot .sp-banner { margin-bottom: 12px; }
  262. #spRoot .sp-banner.bad { color: var(--ss-danger); }
  263. </style>
  264. <div id="spRoot">
  265. <div class="ss-page-desc" locale="sp/desc">Manage where files are stored and who can use them.</div>
  266. <div id="spActions" data-ss-page-actions>
  267. <div class="ss-segmented" id="spViewSwitch">
  268. <button type="button" class="active" data-view="drives" locale="sp/view/drives">Drives</button>
  269. <button type="button" data-view="access" locale="sp/view/access">Who can access</button>
  270. </div>
  271. <button type="button" class="ss-btn primary" id="spAddDrive" locale="sp/add">Add drive</button>
  272. </div>
  273. <div id="spBanners"></div>
  274. <div id="spDrivesView">
  275. <div id="spDriveSections"><div class="ss-card"><div class="ss-empty" locale="sp/loading">Loading</div></div></div>
  276. <div class="ss-section">
  277. <div class="ss-section-title" locale="sp/advanced">Advanced</div>
  278. <div class="ss-card">
  279. <div class="ss-row">
  280. <div class="ss-row-text">
  281. <div class="ss-row-label" locale="sp/reload">Reload storage pools</div>
  282. <div class="ss-row-desc" locale="sp/reload/desc">Closes and reopens drives from the config files. Only needed after editing those files by hand. Open files on these drives are interrupted.</div>
  283. </div>
  284. <div class="ss-control">
  285. <select class="ss-select" id="spReloadPool"></select>
  286. <button type="button" class="ss-btn danger-outline" id="spReload" locale="sp/reload/button">Reload</button>
  287. </div>
  288. </div>
  289. </div>
  290. </div>
  291. </div>
  292. <div id="spAccessView" style="display:none;">
  293. <div class="ss-section">
  294. <div class="ss-card">
  295. <div class="sp-wide sp-matrix-wrap" id="spMatrix"></div>
  296. <div class="sp-narrow">
  297. <div class="ss-row">
  298. <div class="ss-row-text"><div class="ss-row-label" locale="sp/narrow/group">Group</div></div>
  299. <div class="ss-control"><select class="ss-select" id="spNarrowGroup"></select></div>
  300. </div>
  301. <div id="spNarrowList"></div>
  302. </div>
  303. </div>
  304. </div>
  305. <div class="ss-note sp-note-block"><span locale="sp/access/how">A group set to Use default gets the Default access level. If a user's groups have different levels, the highest one applies. Changes are saved as you make them.</span></div>
  306. <div class="ss-section">
  307. <div class="ss-section-title" locale="sp/check">Check a user</div>
  308. <div class="ss-card">
  309. <div class="sp-check">
  310. <input type="text" class="ss-input" id="spCheckUser" placeholder="Username">
  311. <select class="ss-select" id="spCheckDrive"></select>
  312. <button type="button" class="ss-btn" id="spCheckButton" locale="sp/check/button">Check</button>
  313. </div>
  314. <div class="sp-check-result" id="spCheckResult"></div>
  315. </div>
  316. </div>
  317. </div>
  318. <div id="spPanelBack">
  319. <div id="spPanel" role="dialog" aria-modal="true" aria-labelledby="spPanelTitle">
  320. <div class="sp-panel-head">
  321. <div>
  322. <div class="sp-panel-title" id="spPanelTitle"></div>
  323. <div class="sp-panel-sub" id="spPanelSub"></div>
  324. </div>
  325. <button type="button" class="sp-close" id="spPanelClose" aria-label="Close">&times;</button>
  326. </div>
  327. <div class="sp-panel-body" id="spPanelBody"></div>
  328. <div class="sp-panel-foot" id="spPanelFoot"></div>
  329. </div>
  330. </div>
  331. </div>
  332. <script>
  333. (function () {
  334. /*
  335. Storage Pools
  336. Lists every drive of this host and who may use it. The server works out
  337. each drive's state and rule (/system/storage/drives), this page only
  338. shows them. A drive's access list holds a level per permission group
  339. plus a default access level; see mod/storage/access.
  340. Own locale instance rather than the shared global one, see the File
  341. Manager settings page for why.
  342. */
  343. var spLocale = (typeof NewAppLocale === "function") ? NewAppLocale() : {
  344. init: function (f, cb) { cb(); },
  345. translate: function () {},
  346. getString: function (k, fallback) { return fallback; }
  347. };
  348. function T(key, fallback) {
  349. return (spLocale && spLocale.getString) ? spLocale.getString(key, fallback) : fallback;
  350. }
  351. function fill(text, values) {
  352. Object.keys(values).forEach(function (name) {
  353. text = text.split("{" + name + "}").join(values[name]);
  354. });
  355. return text;
  356. }
  357. function esc(value) {
  358. return String(value === undefined || value === null ? "" : value)
  359. .replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;")
  360. .replace(/"/g, "&quot;").replace(/'/g, "&#39;");
  361. }
  362. function notify(text, ok) {
  363. if (typeof msgbox === "function") {
  364. msgbox(esc(text), ok !== false);
  365. }
  366. }
  367. var URLS = {
  368. drives: "../../system/storage/drives",
  369. access: "../../system/storage/access",
  370. explain: "../../system/storage/access/explain",
  371. edit: "../../system/storage/pool/edit",
  372. test: "../../system/storage/pool/test",
  373. toggle: "../../system/storage/pool/toggle",
  374. remove: "../../system/storage/pool/removeHandler",
  375. reload: "../../system/storage/pool/reload",
  376. devices: "../../system/disk/diskmg/devices"
  377. };
  378. var SYSTEM_POOL = "system";
  379. var RELOAD_ALL = "1eb201a3-d0f6-6630-5e6d-2f40480115c5";
  380. var NEW_DRIVE = "__new__"; //Stands for the drive being added, which has no id yet
  381. var INHERIT = "inherit"; //A group with no row: it follows the default access
  382. var ID_FORMAT = /^[A-Za-z0-9_-]+$/;
  383. var LOCAL_FILESYSTEMS = ["ext4", "ext3", "ext2", "ntfs", "vfat", "fat"];
  384. var NETWORK_FILESYSTEMS = ["smb", "webdav", "ftp", "sftp", "s3"];
  385. var groups = [];
  386. var drives = [];
  387. var panelDriveId = null; //The drive whose access list the panel shows
  388. var wizard = null; //The add or edit drive form, when open
  389. /* Names and levels */
  390. function levelName(drive, level) {
  391. if (drive.uuid === "share") {
  392. return level === "denied" ? T("sp/level/hidden", "Hidden") : T("sp/level/visible", "Visible");
  393. }
  394. if (level === "readwrite") return T("sp/level/readwrite", "Read & write");
  395. if (level === "readonly") return T("sp/level/read", "Read");
  396. return T("sp/level/none", "None");
  397. }
  398. function levelsOf(drive) {
  399. if (drive.uuid === "share") return ["denied", "readonly"];
  400. return drive.readOnly ? ["denied", "readonly"] : ["denied", "readonly", "readwrite"];
  401. }
  402. function topLevelOf(drive) {
  403. return (drive.readOnly || drive.uuid === "share") ? "readonly" : "readwrite";
  404. }
  405. function driveName(drive) {
  406. if (drive.uuid === "user") return T("sp/name/user", "Home folders");
  407. if (drive.uuid === "tmp") return T("sp/name/tmp", "Temporary files");
  408. if (drive.uuid === "share") return T("sp/name/share", "Shared with me");
  409. if (drive.uuid === "cluster") return T("sp/name/cluster", "Cluster");
  410. return drive.name;
  411. }
  412. function typeName(drive) {
  413. var names = { smb: "SMB", webdav: "WebDAV", ftp: "FTP", sftp: "SFTP", s3: "S3" };
  414. if (names[drive.filesystem]) return names[drive.filesystem];
  415. return (drive.kind === "configured") ? T("sp/type/local", "Local") : "";
  416. }
  417. function stateBadge(drive) {
  418. var states = {
  419. online: ["success", T("sp/state/online", "Online")],
  420. stopped: ["warning", T("sp/state/stopped", "Stopped")],
  421. offline: ["danger", T("sp/state/offline", "Offline")],
  422. removed: ["danger", T("sp/state/removed", "Removed")],
  423. notactive: ["", T("sp/state/notactive", "Not active")]
  424. };
  425. var state = states[drive.state] || ["", drive.state];
  426. return '<span class="ss-badge ' + state[0] + '">' + esc(state[1]) + "</span>";
  427. }
  428. function iconOf(drive) {
  429. if (drive.state === "offline" || drive.state === "notactive") return "drive-notfound.svg";
  430. if (drive.kind === "fixed") return "drive.svg";
  431. return drive.hierarchy === "public" ? "drive-public.svg" : "drive-virtual.svg";
  432. }
  433. // Stopped, offline and the like: a drive that is not serving files right now
  434. function isInactive(drive) {
  435. return drive.state !== "online";
  436. }
  437. function homeGroupOf(drive) {
  438. return drive.pool === SYSTEM_POOL ? "" : drive.pool;
  439. }
  440. // Why a group cannot be set on a drive, or "" when it can
  441. function lockOf(drive, group) {
  442. if (drive.pool === group.name) return "home";
  443. if (group.isAdmin && drive.pool === SYSTEM_POOL) return "admin";
  444. return "";
  445. }
  446. function ruleRows(drive) {
  447. return drive.rule.groups || {};
  448. }
  449. // Who can use a drive, in one line
  450. function summaryOf(drive) {
  451. if (drive.kind === "fixed") {
  452. return fill(T("sp/who/everyone", "Everyone: {level}"), { level: levelName(drive, "readwrite") });
  453. }
  454. var parts = [];
  455. var home = homeGroupOf(drive);
  456. if (home !== "") {
  457. parts.push(fill(T("sp/who/home", "{group} (home group)"), { group: home }));
  458. }
  459. var rows = ruleRows(drive);
  460. Object.keys(rows).sort().forEach(function (group) {
  461. parts.push(group + ": " + levelName(drive, rows[group]));
  462. });
  463. if (drive.rule.other !== "denied") {
  464. parts.unshift(fill(T("sp/who/everyone", "Everyone: {level}"), { level: levelName(drive, drive.rule.other) }));
  465. }
  466. if (parts.length === 0) {
  467. return T("sp/who/admins", "Admins only");
  468. }
  469. return parts.join(", ");
  470. }
  471. /* The list of drives */
  472. function loadDrives() {
  473. return $.get(URLS.drives, function (data) {
  474. if (data && data.error !== undefined) {
  475. $("#spDriveSections").html('<div class="ss-card"><div class="ss-empty">' + esc(data.error) + "</div></div>");
  476. return;
  477. }
  478. groups = data.groups || [];
  479. drives = data.drives || [];
  480. renderBanners(data.ruleError);
  481. renderDrives();
  482. renderMatrix();
  483. renderNarrow();
  484. renderReloadPools();
  485. renderCheckDrives();
  486. if (panelDriveId !== null) {
  487. renderAccessPanel();
  488. }
  489. });
  490. }
  491. function findDrive(uuid) {
  492. if (uuid === NEW_DRIVE) return wizard.drive;
  493. for (var i = 0; i < drives.length; i++) {
  494. if (drives[i].uuid === uuid) return drives[i];
  495. }
  496. return null;
  497. }
  498. function renderBanners(ruleError) {
  499. var html = "";
  500. if (ruleError) {
  501. html += '<div class="ss-note sp-banner bad"><span>' +
  502. esc(fill(T("sp/rule/unreadable", "The access rules could not be read ({reason}), so every drive is closed to everyone but admins until the file is repaired."), { reason: ruleError })) +
  503. "</span></div>";
  504. }
  505. var duplicates = {};
  506. drives.forEach(function (drive) {
  507. if (drive.duplicate) duplicates[drive.uuid] = true;
  508. });
  509. var ids = Object.keys(duplicates);
  510. if (ids.length > 0) {
  511. html += '<div class="ss-note sp-banner"><span>' +
  512. esc(fill(T("sp/duplicate/note", "These drive ids are used by more than one pool: {ids}. An access list applies to every drive of that id, so give each drive its own id."), { ids: ids.join(", ") })) +
  513. "</span></div>";
  514. }
  515. $("#spBanners").html(html);
  516. }
  517. function driveRow(drive) {
  518. var type = typeName(drive);
  519. var title = '<span>' + esc(driveName(drive)) + '</span><span class="sp-id">' + esc(drive.uuid) + ":/</span>";
  520. if (type !== "") title += '<span class="ss-badge">' + esc(type) + "</span>";
  521. title += stateBadge(drive);
  522. if (drive.duplicate) title += '<span class="ss-badge warning">' + esc(T("sp/duplicate/badge", "Duplicate id")) + "</span>";
  523. if (drive.readOnly && drive.uuid !== "share") title += '<span class="ss-badge">' + esc(T("sp/readonly/badge", "Read only")) + "</span>";
  524. var actions = "";
  525. if (drive.kind === "fixed") {
  526. actions = '<span class="sp-fixed">' + esc(T("sp/fixed", "Fixed")) + "</span>";
  527. } else {
  528. actions = '<button type="button" class="ss-btn" data-action="access" data-uuid="' + esc(drive.uuid) + '">' + esc(T("sp/access", "Access")) + "</button>";
  529. }
  530. if (drive.kind === "configured") {
  531. var live = drive.state === "online" || drive.state === "stopped";
  532. actions += '<div class="sp-menu-wrap">' +
  533. '<button type="button" class="ss-btn" data-action="menu" aria-label="' + esc(T("sp/more", "More")) + '">...</button>' +
  534. '<div class="sp-menu">' +
  535. '<button type="button" data-action="edit" data-uuid="' + esc(drive.uuid) + '" data-pool="' + esc(drive.pool) + '">' + esc(T("sp/edit", "Edit")) + "</button>" +
  536. (live ? '<button type="button" data-action="toggle" data-uuid="' + esc(drive.uuid) + '" data-pool="' + esc(drive.pool) + '" title="' + esc(T("sp/stop/hint", "Lasts until the next restart or pool reload")) + '">' +
  537. esc(drive.state === "stopped" ? T("sp/start", "Start") : T("sp/stop", "Stop")) + "</button>" : "") +
  538. '<button type="button" class="danger" data-action="remove" data-uuid="' + esc(drive.uuid) + '" data-pool="' + esc(drive.pool) + '">' + esc(T("sp/remove", "Remove")) + "</button>" +
  539. "</div></div>";
  540. }
  541. var who = esc(summaryOf(drive));
  542. if (drive.state === "notactive") {
  543. who = esc(T("sp/cluster/inactive", "Appears once this node is in a cluster with a volume.")) +
  544. ' <button type="button" class="sp-link" data-action="clustersettings">' + esc(T("sp/cluster/settings", "Cluster Settings")) + "</button>";
  545. }
  546. return '<div class="sp-drive' + (isInactive(drive) ? " inactive" : "") + '">' +
  547. '<img class="sp-drive-icon" alt="" src="../../img/system/' + iconOf(drive) + '">' +
  548. '<div class="sp-drive-main"><div class="sp-drive-title">' + title + '</div><div class="sp-drive-who">' + who + "</div></div>" +
  549. '<div class="sp-drive-actions">' + actions + "</div></div>";
  550. }
  551. function section(title, rows) {
  552. if (rows.length === 0) return "";
  553. return '<div class="ss-section"><div class="ss-section-title">' + esc(title) + '</div><div class="ss-card">' + rows.map(driveRow).join("") + "</div></div>";
  554. }
  555. function renderDrives() {
  556. var builtin = drives.filter(function (d) { return d.kind !== "configured"; });
  557. var system = drives.filter(function (d) { return d.kind === "configured" && d.pool === SYSTEM_POOL; });
  558. var html = section(T("sp/section/builtin", "Built-in"), builtin);
  559. html += section(T("sp/section/system", "System pool"), system);
  560. groups.forEach(function (group) {
  561. var rows = drives.filter(function (d) { return d.kind === "configured" && d.pool === group.name; });
  562. html += section(fill(T("sp/section/group", "Group pool: {group}"), { group: group.name }), rows);
  563. });
  564. $("#spDriveSections").html(html);
  565. }
  566. function renderReloadPools() {
  567. var html = '<option value="' + RELOAD_ALL + '">' + esc(T("sp/reload/all", "All pools")) + "</option>" +
  568. '<option value="' + SYSTEM_POOL + '">' + esc(T("sp/section/system", "System pool")) + "</option>";
  569. groups.forEach(function (group) {
  570. html += '<option value="' + esc(group.name) + '">' + esc(group.name) + "</option>";
  571. });
  572. var current = $("#spReloadPool").val();
  573. $("#spReloadPool").html(html);
  574. if (current) $("#spReloadPool").val(current);
  575. }
  576. /* Access levels: one control shared by the matrix, the phone list and the panel */
  577. function accessDrives() {
  578. return drives.filter(function (d) { return d.kind !== "fixed"; }).filter(function (drive, index, list) {
  579. //A duplicate id has one access list, so it gets one row
  580. return list.findIndex(function (other) { return other.uuid === drive.uuid; }) === index;
  581. });
  582. }
  583. function lockedCell(drive, lock) {
  584. var reason = lock === "home" ? T("sp/lock/home", "Home group") : T("sp/lock/admin", "Admin");
  585. return '<span class="sp-locked">' + esc(levelName(drive, topLevelOf(drive))) + "<small>" + esc(reason) + "</small></span>";
  586. }
  587. // The control of one group, or of the default access when group is null
  588. function levelControl(drive, group) {
  589. if (group !== null) {
  590. var lock = lockOf(drive, group);
  591. if (lock !== "") return lockedCell(drive, lock);
  592. }
  593. var current = group === null ? drive.rule.other : ruleRows(drive)[group.name];
  594. var inherits = group !== null && current === undefined;
  595. if (current === "readwrite" && topLevelOf(drive) !== "readwrite") current = "readonly";
  596. var options = "";
  597. if (group !== null) {
  598. options += '<option value="' + INHERIT + '"' + (inherits ? " selected" : "") + ">" + esc(T("sp/level/inherit", "Use default")) + "</option>";
  599. }
  600. levelsOf(drive).forEach(function (level) {
  601. options += '<option value="' + level + '"' + (level === current ? " selected" : "") + ">" + esc(levelName(drive, level)) + "</option>";
  602. });
  603. return '<select class="ss-select sp-level' + (inherits ? " inherit" : "") + '"' +
  604. ' data-uuid="' + esc(drive.uuid) + '" data-group="' + esc(group === null ? "" : group.name) + '"' +
  605. (drive.ruleEditable ? "" : " disabled") +
  606. ' aria-label="' + esc(driveName(drive) + ", " + (group === null ? T("sp/everyone", "Default access") : group.name)) + '">' + options + "</select>";
  607. }
  608. function renderMatrix() {
  609. var list = accessDrives();
  610. if (list.length === 0) {
  611. $("#spMatrix").html('<div class="ss-empty">' + esc(T("sp/nodrives", "No drives to set access for yet.")) + "</div>");
  612. return;
  613. }
  614. //The default leads: the groups read as exceptions to it, and it stays
  615. //in view when there are more groups than fit
  616. var html = '<table class="sp-matrix"><thead><tr><th scope="col">' + esc(T("sp/drive", "Drive")) + "</th>" +
  617. '<th scope="col">' + esc(T("sp/everyone", "Default access")) + "</th>";
  618. groups.forEach(function (group) {
  619. html += '<th scope="col">' + esc(group.name) + "</th>";
  620. });
  621. html += "</tr></thead><tbody>";
  622. list.forEach(function (drive) {
  623. html += "<tr" + (isInactive(drive) ? ' class="inactive"' : "") + '><th scope="row">' + esc(driveName(drive)) + '<span class="sp-id">' + esc(drive.uuid) + ":/</span></th>" +
  624. "<td>" + levelControl(drive, null) + "</td>";
  625. groups.forEach(function (group) {
  626. html += "<td>" + levelControl(drive, group) + "</td>";
  627. });
  628. html += "</tr>";
  629. });
  630. $("#spMatrix").html(html + "</tbody></table>");
  631. }
  632. function renderNarrow() {
  633. var current = $("#spNarrowGroup").val();
  634. var html = "";
  635. groups.forEach(function (group) {
  636. html += '<option value="' + esc(group.name) + '">' + esc(group.name) + "</option>";
  637. });
  638. html += '<option value="">' + esc(T("sp/everyone", "Default access")) + "</option>";
  639. $("#spNarrowGroup").html(html);
  640. if (current !== null && current !== undefined) $("#spNarrowGroup").val(current);
  641. renderNarrowList();
  642. }
  643. function renderNarrowList() {
  644. var name = $("#spNarrowGroup").val();
  645. var group = null;
  646. groups.forEach(function (g) {
  647. if (g.name === name) group = g;
  648. });
  649. var html = "";
  650. accessDrives().forEach(function (drive) {
  651. html += '<div class="sp-rule-row' + (isInactive(drive) ? " inactive" : "") + '"><span>' + esc(driveName(drive)) + ' <span class="sp-id">' + esc(drive.uuid) + ":/</span></span>" + levelControl(drive, group) + "</div>";
  652. });
  653. $("#spNarrowList").html(html);
  654. }
  655. function changeLevel(select) {
  656. var uuid = $(select).attr("data-uuid");
  657. var groupName = $(select).attr("data-group");
  658. var value = $(select).val();
  659. var drive = findDrive(uuid);
  660. if (drive === null) return;
  661. var rule = { other: drive.rule.other, groups: $.extend({}, ruleRows(drive)) };
  662. if (groupName === "") {
  663. rule.other = value;
  664. } else if (value === INHERIT) {
  665. delete rule.groups[groupName];
  666. } else {
  667. rule.groups[groupName] = value;
  668. }
  669. if (uuid === NEW_DRIVE) {
  670. drive.rule = rule;
  671. renderWizard();
  672. return;
  673. }
  674. $.post(URLS.access, { uuid: uuid, rule: JSON.stringify(rule) }, function (data) {
  675. if (data && data.error !== undefined) {
  676. notify(data.error, false);
  677. } else {
  678. notify(T("sp/saved", "Saved"));
  679. }
  680. loadDrives();
  681. }).fail(function () {
  682. notify(T("sp/failed", "The server did not answer"), false);
  683. loadDrives();
  684. });
  685. }
  686. /* Check a user */
  687. function renderCheckDrives() {
  688. var current = $("#spCheckDrive").val();
  689. var html = "";
  690. accessDrives().forEach(function (drive) {
  691. html += '<option value="' + esc(drive.uuid) + '">' + esc(driveName(drive)) + " (" + esc(drive.uuid) + ":/)</option>";
  692. });
  693. $("#spCheckDrive").html(html);
  694. if (current) $("#spCheckDrive").val(current);
  695. }
  696. function explainText(user, drive, decision) {
  697. var values = { user: user, level: levelName(drive, decision.level), drive: drive.uuid + ":/", group: decision.group || "" };
  698. var reasons = {
  699. home: T("sp/why/home", "{user} has {level} on {drive}, as a member of its home group {group}."),
  700. admin: T("sp/why/admin", "{user} has {level} on {drive}, as an admin."),
  701. group: T("sp/why/group", "{user} has {level} on {drive}, from the row of group {group}."),
  702. other: T("sp/why/other", "{user} has {level} on {drive}, from the default access.")
  703. };
  704. var text = fill(reasons[decision.reason] || reasons.other, values);
  705. if (decision.capped) text += " " + T("sp/why/capped", "The drive itself is read only.");
  706. return text;
  707. }
  708. function checkUser() {
  709. var user = $("#spCheckUser").val().trim();
  710. var uuid = $("#spCheckDrive").val();
  711. if (user === "" || !uuid) return;
  712. $.get(URLS.explain, { user: user, drive: uuid }, function (data) {
  713. if (data && data.error !== undefined) {
  714. $("#spCheckResult").text(data.error);
  715. return;
  716. }
  717. $("#spCheckResult").text(explainText(user, findDrive(uuid), data));
  718. });
  719. }
  720. /* The panel */
  721. function openPanel() {
  722. $("#spPanelBack").addClass("open");
  723. focusPanel();
  724. }
  725. function focusPanel() {
  726. $("#spPanelBody").find("input:not([readonly]), select, button").first().trigger("focus");
  727. }
  728. function closePanel() {
  729. $("#spPanelBack").removeClass("open");
  730. panelDriveId = null;
  731. wizard = null;
  732. }
  733. function renderAccessPanel() {
  734. var drive = findDrive(panelDriveId);
  735. if (drive === null) {
  736. closePanel();
  737. return;
  738. }
  739. $("#spPanelTitle").text(fill(T("sp/panel/access", "Who can use {drive}"), { drive: drive.uuid + ":/" }));
  740. $("#spPanelSub").text(driveName(drive));
  741. var html = "";
  742. if (!drive.ruleEditable) {
  743. html += '<div class="ss-note sp-note-block"><span>' + esc(T("sp/cluster/noedit", "The access list of cluster:/ belongs to the cluster, so it can only be changed on a node that is in one.")) + "</span></div>";
  744. } else if (drive.ruleReplicated) {
  745. html += '<div class="ss-note sp-note-block"><span>' + esc(T("sp/cluster/replicated", "This list applies on every node of the cluster. Groups are matched by name.")) + "</span></div>";
  746. }
  747. html += '<div class="ss-card">' + accessRows(drive) + "</div>";
  748. $("#spPanelBody").html(html);
  749. $("#spPanelFoot").html('<button type="button" class="ss-btn" data-action="closepanel">' + esc(T("sp/done", "Done")) + "</button>");
  750. }
  751. function accessRows(drive) {
  752. var html = '<div class="sp-rule-row"><span>' + esc(T("sp/everyone", "Default access")) + "</span>" + levelControl(drive, null) + "</div>";
  753. groups.forEach(function (group) {
  754. html += '<div class="sp-rule-row"><span>' + esc(group.name) + "</span>" + levelControl(drive, group) + "</div>";
  755. });
  756. //A rule of cluster:/ can name a group that only exists on other nodes
  757. var known = groups.map(function (g) { return g.name; });
  758. Object.keys(ruleRows(drive)).sort().forEach(function (name) {
  759. if (known.indexOf(name) >= 0) return;
  760. html += '<div class="sp-rule-row"><span>' + esc(name) + '<div class="sp-hint">' + esc(T("sp/group/missing", "This group does not exist on this node")) + "</div></span>" +
  761. '<span class="sp-locked">' + esc(levelName(drive, ruleRows(drive)[name])) + "</span></div>";
  762. });
  763. return html;
  764. }
  765. function openAccessPanel(uuid) {
  766. panelDriveId = uuid;
  767. wizard = null;
  768. renderAccessPanel();
  769. openPanel();
  770. }
  771. /* Adding and editing a drive */
  772. function newOption() {
  773. return {
  774. name: "", uuid: "", path: "", access: "readwrite", hierarchy: "public", automount: false,
  775. filesystem: "", mountdev: "", mountpt: "", diskuuid: "", username: "", password: ""
  776. };
  777. }
  778. function openAddDrive() {
  779. panelDriveId = null;
  780. wizard = {
  781. mode: "new", step: 1, kind: "", pool: SYSTEM_POOL, idTouched: false, pathTouched: false,
  782. option: newOption(), error: "", fieldErrors: {},
  783. drive: { uuid: NEW_DRIVE, pool: SYSTEM_POOL, kind: "configured", ruleEditable: true, readOnly: false, rule: { other: "readwrite" } }
  784. };
  785. renderWizard();
  786. openPanel();
  787. }
  788. function openEditDrive(uuid, pool) {
  789. $.post(URLS.edit, { opr: "get", group: pool, uuid: uuid }, function (data) {
  790. if (data && data.error !== undefined) {
  791. notify(data.error, false);
  792. return;
  793. }
  794. panelDriveId = null;
  795. var option = $.extend(newOption(), data);
  796. var kind = "folder";
  797. if (NETWORK_FILESYSTEMS.indexOf(option.filesystem) >= 0) {
  798. kind = "network";
  799. } else if (option.automount || option.mountdev) {
  800. kind = "disk";
  801. }
  802. wizard = {
  803. mode: "edit", step: 2, kind: kind, pool: pool, idTouched: true, pathTouched: true,
  804. option: option, error: "", fieldErrors: {}, drive: null
  805. };
  806. renderWizard();
  807. openPanel();
  808. });
  809. }
  810. function suggestId(name) {
  811. return name.trim().toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
  812. }
  813. function field(name, label, value, options) {
  814. options = options || {};
  815. var error = wizard.fieldErrors[name] || "";
  816. return '<div class="sp-field"><label for="spField_' + name + '">' + esc(label) + "</label>" +
  817. '<input class="ss-input' + (error ? " invalid" : "") + '" id="spField_' + name + '" data-field="' + name + '"' +
  818. ' type="' + (options.type || "text") + '" value="' + esc(value) + '"' +
  819. (options.placeholder ? ' placeholder="' + esc(options.placeholder) + '"' : "") +
  820. (options.readonly ? " readonly" : "") + ' autocomplete="off">' +
  821. (options.hint ? '<div class="sp-hint">' + esc(options.hint) + "</div>" : "") +
  822. '<div class="sp-error">' + esc(error) + "</div></div>";
  823. }
  824. function selectField(name, label, value, choices, hint) {
  825. var options = "";
  826. choices.forEach(function (choice) {
  827. options += '<option value="' + esc(choice[0]) + '"' + (choice[0] === value ? " selected" : "") + ">" + esc(choice[1]) + "</option>";
  828. });
  829. return '<div class="sp-field"><label for="spField_' + name + '">' + esc(label) + "</label>" +
  830. '<select class="ss-select" id="spField_' + name + '" data-field="' + name + '">' + options + "</select>" +
  831. (hint ? '<div class="sp-hint">' + esc(hint) + "</div>" : "") + "</div>";
  832. }
  833. function switchField(name, label, hint, checked) {
  834. return '<div class="sp-switch-row"><div><b>' + esc(label) + '</b><span class="sp-hint">' + esc(hint) + "</span></div>" +
  835. '<label class="ss-switch"><input type="checkbox" data-field="' + name + '"' + (checked ? " checked" : "") + ' aria-label="' + esc(label) + '"><span class="ss-track"></span></label></div>';
  836. }
  837. function stepOne() {
  838. function choice(kind, icon, title, text) {
  839. return '<button type="button" class="sp-choice" data-action="kind" data-kind="' + kind + '">' +
  840. '<img alt="" src="../../img/system/' + icon + '"><div><b>' + esc(title) + "</b><span>" + esc(text) + "</span></div></button>";
  841. }
  842. return choice("folder", "drive-virtual.svg", T("sp/kind/folder", "A folder on this server"), T("sp/kind/folder/desc", "Any folder this server can already reach."))
  843. + choice("disk", "drive.svg", T("sp/kind/disk", "A physical disk"), T("sp/kind/disk/desc", "A partition that ArozOS mounts for you."))
  844. + choice("network", "drive-public.svg", T("sp/kind/network", "A network drive"), T("sp/kind/network/desc", "SMB, WebDAV, FTP, SFTP or S3 storage on another machine."));
  845. }
  846. function networkHint(filesystem) {
  847. var hints = {
  848. smb: "192.168.1.10/share",
  849. webdav: "https://example.com/dav",
  850. ftp: "192.168.1.10:21/folder",
  851. sftp: "192.168.1.10:22/folder",
  852. s3: "s3.amazonaws.com/my-bucket"
  853. };
  854. return hints[filesystem] || "";
  855. }
  856. function stepDetails() {
  857. var o = wizard.option;
  858. var html = "";
  859. html += field("name", T("sp/field/name", "Name"), o.name, { placeholder: T("sp/field/name/placeholder", "e.g. Photos") });
  860. html += field("uuid", T("sp/field/id", "Drive ID"), o.uuid, {
  861. readonly: wizard.mode === "edit",
  862. hint: wizard.mode === "edit"
  863. ? fill(T("sp/field/id/locked", "Files on this drive are at {path}. The id cannot be changed."), { path: o.uuid + ":/" })
  864. : fill(T("sp/field/id/hint", "Files on this drive will be at {path}"), { path: (o.uuid || "id") + ":/" })
  865. });
  866. if (wizard.kind === "folder") {
  867. html += field("path", T("sp/field/path", "Folder"), o.path, { placeholder: T("sp/field/path/placeholder", "e.g. /media/photos") });
  868. html += testRow();
  869. } else if (wizard.kind === "disk") {
  870. html += '<div class="sp-field"><label for="spField_mountdev">' + esc(T("sp/field/device", "Device")) + "</label>" +
  871. '<div class="sp-inline"><input class="ss-input" id="spField_mountdev" data-field="mountdev" value="' + esc(o.mountdev) + '" placeholder="/dev/sda1" autocomplete="off">' +
  872. '<button type="button" class="ss-btn" data-action="devices">' + esc(T("sp/field/browse", "Browse")) + "</button></div>" +
  873. '<div id="spDevices"></div></div>';
  874. html += selectField("filesystem", T("sp/field/filesystem", "File system"), o.filesystem || "ext4", LOCAL_FILESYSTEMS.map(function (fsType) {
  875. return [fsType, fsType.toUpperCase()];
  876. }));
  877. html += field("diskuuid", T("sp/field/partition", "Partition UUID"), o.diskuuid, { hint: T("sp/field/partition/hint", "Recommended for USB drives: it survives device renames across reboots.") });
  878. html += field("mountpt", T("sp/field/mountpoint", "Mount point"), o.mountpt, { placeholder: "/media/photos" });
  879. html += field("path", T("sp/field/diskpath", "Folder to use"), o.path, { hint: T("sp/field/diskpath/hint", "Usually the mount point.") });
  880. html += switchField("automount", T("sp/field/automount", "Mount at startup"), T("sp/field/automount/hint", "ArozOS mounts the device when it starts."), o.automount);
  881. } else if (wizard.kind === "network") {
  882. var isS3 = o.filesystem === "s3";
  883. html += selectField("filesystem", T("sp/field/protocol", "Protocol"), o.filesystem, [["smb", "SMB"], ["webdav", "WebDAV"], ["ftp", "FTP"], ["sftp", "SFTP"], ["s3", T("sp/field/s3", "S3 / S3-compatible")]]);
  884. html += field("path", T("sp/field/server", "Server address"), o.path, {
  885. placeholder: networkHint(o.filesystem),
  886. hint: isS3 ? T("sp/field/s3/hint", "Add http:// in front for a server without SSL, such as a local MinIO.") : ""
  887. });
  888. html += field("username", isS3 ? T("sp/field/accesskey", "Access key ID") : T("sp/field/username", "Username"), o.username);
  889. html += field("password", isS3 ? T("sp/field/secretkey", "Secret access key") : T("sp/field/password", "Password"), o.password, {
  890. type: "password",
  891. hint: wizard.mode === "edit" ? T("sp/field/login/keep", "Leave both empty to keep the saved login.") : ""
  892. });
  893. html += testRow();
  894. }
  895. html += selectField("hierarchy", T("sp/field/layout", "Folder layout"), o.hierarchy, [
  896. ["public", T("sp/field/layout/public", "One shared folder")],
  897. ["user", T("sp/field/layout/user", "A private folder per user")]
  898. ], o.hierarchy === "user"
  899. ? T("sp/field/layout/user/hint", "Each user only sees their own folder on this drive.")
  900. : T("sp/field/layout/public/hint", "Everyone who can use the drive sees the same files."));
  901. html += switchField("readonly", T("sp/field/readonly", "Read only drive"), T("sp/field/readonly/hint", "Nobody can change files on this drive, whatever its access list says."), o.access === "readonly");
  902. return html;
  903. }
  904. function testRow() {
  905. return '<div class="sp-field sp-inline" style="align-items:center;">' +
  906. '<button type="button" class="ss-btn" data-action="test">' + esc(T("sp/test", "Test")) + "</button>" +
  907. '<span class="sp-test-result" id="spTestResult"></span></div>';
  908. }
  909. function stepAccess() {
  910. var choices = [[SYSTEM_POOL, T("sp/field/pool/system", "System pool")]];
  911. groups.forEach(function (group) {
  912. choices.push([group.name, fill(T("sp/field/pool/group", "Group pool: {group}"), { group: group.name })]);
  913. });
  914. wizard.drive.pool = wizard.pool;
  915. wizard.drive.readOnly = wizard.option.access === "readonly";
  916. return selectField("pool", T("sp/field/pool", "Belongs to"), wizard.pool, choices, T("sp/field/pool/hint", "Members of a group always have full access to the drives of its pool."))
  917. + '<div class="ss-card">' + accessRows(wizard.drive) + "</div>";
  918. }
  919. function renderWizard() {
  920. if (wizard === null) return;
  921. var isNew = wizard.mode === "new";
  922. $("#spPanelTitle").text(isNew ? T("sp/panel/add", "Add drive") : fill(T("sp/panel/edit", "Edit {drive}"), { drive: wizard.option.uuid + ":/" }));
  923. $("#spPanelSub").text(isNew ? fill(T("sp/panel/step", "Step {step} of 3"), { step: wizard.step }) : "");
  924. var body = '<div class="sp-panel-error" id="spPanelError">' + esc(wizard.error) + "</div>";
  925. var foot = "";
  926. if (wizard.step === 1) {
  927. body += stepOne();
  928. foot = '<button type="button" class="ss-btn" data-action="closepanel">' + esc(T("sp/cancel", "Cancel")) + "</button>";
  929. } else if (wizard.step === 2) {
  930. body += stepDetails();
  931. foot = (isNew ? '<button type="button" class="ss-btn" data-action="back">' + esc(T("sp/back", "Back")) + "</button>" : "") +
  932. '<span class="sp-grow"></span>' +
  933. '<button type="button" class="ss-btn" data-action="closepanel">' + esc(T("sp/cancel", "Cancel")) + "</button>" +
  934. '<button type="button" class="ss-btn primary" data-action="next">' + esc(isNew ? T("sp/next", "Next") : T("sp/save", "Save")) + "</button>";
  935. } else {
  936. body += stepAccess();
  937. foot = '<button type="button" class="ss-btn" data-action="back">' + esc(T("sp/back", "Back")) + "</button>" +
  938. '<span class="sp-grow"></span>' +
  939. '<button type="button" class="ss-btn" data-action="closepanel">' + esc(T("sp/cancel", "Cancel")) + "</button>" +
  940. '<button type="button" class="ss-btn primary" data-action="save">' + esc(T("sp/add", "Add drive")) + "</button>";
  941. }
  942. $("#spPanelBody").html(body);
  943. $("#spPanelFoot").html(foot);
  944. }
  945. function readField(input) {
  946. var name = $(input).attr("data-field");
  947. var o = wizard.option;
  948. if (name === "readonly") {
  949. o.access = input.checked ? "readonly" : "readwrite";
  950. } else if (name === "automount") {
  951. o.automount = input.checked;
  952. } else if (name === "pool") {
  953. wizard.pool = $(input).val();
  954. wizard.drive.pool = wizard.pool;
  955. wizard.drive.rule = { other: wizard.pool === SYSTEM_POOL ? "readwrite" : "denied" };
  956. renderWizard();
  957. } else {
  958. o[name] = $(input).val();
  959. }
  960. if (name === "name" && !wizard.idTouched) {
  961. o.uuid = suggestId(o.name);
  962. $("#spField_uuid").val(o.uuid);
  963. } else if (name === "uuid") {
  964. wizard.idTouched = true;
  965. } else if (name === "mountpt" && !wizard.pathTouched) {
  966. o.path = o.mountpt;
  967. $("#spField_path").val(o.path);
  968. } else if (name === "path") {
  969. wizard.pathTouched = true;
  970. }
  971. if (name === "name" || name === "uuid") {
  972. $("#spField_uuid").closest(".sp-field").find(".sp-hint").text(
  973. fill(T("sp/field/id/hint", "Files on this drive will be at {path}"), { path: (o.uuid || "id") + ":/" }));
  974. }
  975. if (name === "filesystem" && wizard.kind === "network") {
  976. renderWizard();
  977. }
  978. if (name === "hierarchy") {
  979. renderWizard();
  980. }
  981. }
  982. function checkDetails() {
  983. var o = wizard.option;
  984. var errors = {};
  985. if (o.name.trim() === "") errors.name = T("sp/error/name", "Give the drive a name");
  986. if (wizard.mode === "new" && !ID_FORMAT.test(o.uuid)) {
  987. errors.uuid = T("sp/error/id", "Use only letters, numbers, - and _");
  988. }
  989. if (o.path.trim() === "") errors.path = T("sp/error/path", "This field is required");
  990. if (wizard.kind === "disk" && o.automount && o.mountpt.trim() === "") {
  991. errors.mountpt = T("sp/error/path", "This field is required");
  992. }
  993. wizard.fieldErrors = errors;
  994. return Object.keys(errors).length === 0;
  995. }
  996. function configToSend() {
  997. var o = $.extend({}, wizard.option);
  998. if (wizard.kind === "folder") {
  999. //The server fills in the file system of a plain folder
  1000. o.filesystem = "";
  1001. o.mountdev = "";
  1002. o.mountpt = "";
  1003. o.diskuuid = "";
  1004. o.automount = false;
  1005. } else if (wizard.kind === "disk" && !o.filesystem) {
  1006. o.filesystem = "ext4";
  1007. }
  1008. return JSON.stringify(o);
  1009. }
  1010. // Put a refusal from the server next to the field it is about
  1011. function showServerError(message) {
  1012. wizard.fieldErrors = {};
  1013. wizard.error = "";
  1014. if (/^Path not exists/.test(message)) {
  1015. wizard.fieldErrors.path = message;
  1016. } else if (/id|uuid/i.test(message) && wizard.mode === "new") {
  1017. wizard.fieldErrors.uuid = message;
  1018. } else {
  1019. wizard.error = message;
  1020. }
  1021. if (wizard.step === 3 && (wizard.fieldErrors.path || wizard.fieldErrors.uuid)) {
  1022. wizard.step = 2;
  1023. }
  1024. renderWizard();
  1025. }
  1026. function saveDrive() {
  1027. var payload = { opr: wizard.mode === "new" ? "new" : "set", group: wizard.pool, config: configToSend() };
  1028. if (wizard.mode === "edit") {
  1029. payload.uuid = wizard.option.uuid;
  1030. } else {
  1031. payload.access = JSON.stringify(wizard.drive.rule);
  1032. }
  1033. $("#spPanelFoot button").prop("disabled", true);
  1034. $.post(URLS.edit, payload, function (data) {
  1035. if (data && data.error !== undefined) {
  1036. showServerError(data.error);
  1037. return;
  1038. }
  1039. if (data && data.mounted === false) {
  1040. notify(fill(T("sp/saved/offline", "Saved, but the drive could not be mounted: {reason}"), { reason: data.reason || "" }), false);
  1041. } else {
  1042. notify(T("sp/saved/drive", "Drive saved"));
  1043. }
  1044. closePanel();
  1045. loadDrives();
  1046. }).fail(function () {
  1047. showServerError(T("sp/failed", "The server did not answer"));
  1048. });
  1049. }
  1050. function testDrive() {
  1051. var $result = $("#spTestResult");
  1052. $result.attr("class", "sp-test-result").text(T("sp/test/running", "Testing"));
  1053. $.post(URLS.test, { config: configToSend(), group: wizard.pool }, function (data) {
  1054. if (data && data.error !== undefined) {
  1055. $result.attr("class", "sp-test-result bad").text(data.error);
  1056. } else {
  1057. $result.attr("class", "sp-test-result ok").text(T("sp/test/ok", "Looks good"));
  1058. }
  1059. }).fail(function () {
  1060. $result.attr("class", "sp-test-result bad").text(T("sp/failed", "The server did not answer"));
  1061. });
  1062. }
  1063. /* The partition picker of a physical disk */
  1064. function sizeText(bytes) {
  1065. if (!bytes) return "0 B";
  1066. var units = ["B", "KB", "MB", "GB", "TB"];
  1067. var i = Math.min(units.length - 1, Math.floor(Math.log(bytes) / Math.log(1024)));
  1068. return Math.round(bytes / Math.pow(1024, i) * 10) / 10 + " " + units[i];
  1069. }
  1070. function listDevices() {
  1071. var $list = $("#spDevices").html('<div class="sp-hint">' + esc(T("sp/loading", "Loading")) + "</div>");
  1072. $.get(URLS.devices, function (data) {
  1073. if (!Array.isArray(data) || data.length === 0) {
  1074. $list.html('<div class="sp-hint">' + esc(data && data.error ? data.error : T("sp/devices/none", "No disks found")) + "</div>");
  1075. return;
  1076. }
  1077. var html = "";
  1078. data.forEach(function (disk) {
  1079. html += '<div class="sp-disk-name">' + esc(disk.name) + (disk.model ? " (" + esc(disk.model) + ")" : "") + ", " + sizeText(disk.size) + "</div>";
  1080. (disk.partitions || []).forEach(function (part) {
  1081. html += '<button type="button" class="sp-partition" data-action="partition"' +
  1082. ' data-dev="' + esc(part.devpath) + '" data-uuid="' + esc(part.uuid || "") + '" data-fs="' + esc(part.fstype || "") + '">' +
  1083. esc(part.name) + ", " + sizeText(part.size) +
  1084. (part.label ? ", " + esc(part.label) : "") + (part.fstype ? ", " + esc(part.fstype) : "") + "</button>";
  1085. });
  1086. });
  1087. $list.html(html);
  1088. }).fail(function () {
  1089. $list.html('<div class="sp-hint">' + esc(T("sp/devices/failed", "Disks can only be listed when hardware management is on.")) + "</div>");
  1090. });
  1091. }
  1092. function pickPartition(button) {
  1093. var o = wizard.option;
  1094. o.mountdev = $(button).attr("data-dev");
  1095. o.diskuuid = $(button).attr("data-uuid");
  1096. var fsType = String($(button).attr("data-fs")).toLowerCase();
  1097. var known = { "ntfs-3g": "ntfs", fat32: "vfat" };
  1098. fsType = known[fsType] || fsType;
  1099. if (LOCAL_FILESYSTEMS.indexOf(fsType) >= 0) o.filesystem = fsType;
  1100. renderWizard();
  1101. }
  1102. /* Actions */
  1103. function confirmRemove(uuid, pool) {
  1104. ssConfirm({
  1105. title: fill(T("sp/remove/title", "Remove {drive}?"), { drive: uuid + ":/" }),
  1106. text: T("sp/remove/text", "ArozOS stops using this drive. The files on it stay where they are."),
  1107. confirmLabel: T("sp/remove", "Remove"),
  1108. cancelLabel: T("sp/cancel", "Cancel"),
  1109. danger: true
  1110. }, function () {
  1111. $.post(URLS.remove, { uuid: uuid, group: pool }, function (data) {
  1112. if (data && data.error !== undefined) {
  1113. notify(data.error, false);
  1114. }
  1115. loadDrives();
  1116. });
  1117. });
  1118. }
  1119. function toggleDrive(uuid, pool) {
  1120. $.post(URLS.toggle, { fsh: uuid, group: pool }, function (data) {
  1121. if (data && data.error !== undefined) {
  1122. notify(data.error, false);
  1123. }
  1124. loadDrives();
  1125. });
  1126. }
  1127. function reloadPools() {
  1128. ssConfirm({
  1129. title: T("sp/reload/title", "Reload storage pools?"),
  1130. text: T("sp/reload/text", "Open files on these drives are interrupted, and a wrong config file can take drives offline."),
  1131. confirmLabel: T("sp/reload/button", "Reload"),
  1132. cancelLabel: T("sp/cancel", "Cancel"),
  1133. danger: true
  1134. }, function () {
  1135. $.post(URLS.reload, { pool: $("#spReloadPool").val() }, function (data) {
  1136. if (data && data.error !== undefined) {
  1137. notify(data.error, false);
  1138. } else {
  1139. notify(T("sp/reload/done", "Storage pools reloaded"));
  1140. }
  1141. loadDrives();
  1142. });
  1143. });
  1144. }
  1145. function closeMenus() {
  1146. $("#spRoot .sp-menu.open").removeClass("open");
  1147. }
  1148. $("#spRoot").on("click", "[data-action]", function (event) {
  1149. var $target = $(this);
  1150. var action = $target.attr("data-action");
  1151. var uuid = $target.attr("data-uuid");
  1152. var pool = $target.attr("data-pool");
  1153. if (action === "menu") {
  1154. var $menu = $target.siblings(".sp-menu");
  1155. var wasOpen = $menu.hasClass("open");
  1156. closeMenus();
  1157. $menu.toggleClass("open", !wasOpen);
  1158. event.stopPropagation();
  1159. return;
  1160. }
  1161. closeMenus();
  1162. switch (action) {
  1163. case "access": openAccessPanel(uuid); break;
  1164. case "edit": openEditDrive(uuid, pool); break;
  1165. case "toggle": toggleDrive(uuid, pool); break;
  1166. case "remove": confirmRemove(uuid, pool); break;
  1167. case "closepanel": closePanel(); break;
  1168. case "clustersettings":
  1169. if (typeof openSubcat === "function") openSubcat({ Group: "Cluster", Name: "Cluster Settings" });
  1170. break;
  1171. case "kind":
  1172. wizard.kind = $target.attr("data-kind");
  1173. wizard.option.filesystem = wizard.kind === "network" ? "smb" : (wizard.kind === "disk" ? "ext4" : "");
  1174. wizard.step = 2;
  1175. renderWizard();
  1176. focusPanel();
  1177. break;
  1178. case "back":
  1179. wizard.error = "";
  1180. wizard.step = wizard.step - 1;
  1181. renderWizard();
  1182. focusPanel();
  1183. break;
  1184. case "next":
  1185. wizard.error = "";
  1186. if (!checkDetails()) {
  1187. renderWizard();
  1188. } else if (wizard.mode === "edit") {
  1189. saveDrive();
  1190. } else {
  1191. wizard.step = 3;
  1192. wizard.drive.rule = { other: wizard.pool === SYSTEM_POOL ? "readwrite" : "denied" };
  1193. renderWizard();
  1194. focusPanel();
  1195. }
  1196. break;
  1197. case "save": saveDrive(); break;
  1198. case "test": testDrive(); break;
  1199. case "devices": listDevices(); break;
  1200. case "partition": pickPartition(this); break;
  1201. }
  1202. });
  1203. $("#spRoot").on("change", ".sp-level", function () { changeLevel(this); });
  1204. $("#spRoot").on("input change", "[data-field]", function () {
  1205. if (wizard !== null) readField(this);
  1206. });
  1207. $("#spViewSwitch").on("click", "button", function () {
  1208. var view = $(this).attr("data-view");
  1209. $("#spViewSwitch button").removeClass("active");
  1210. $(this).addClass("active");
  1211. $("#spDrivesView").toggle(view === "drives");
  1212. $("#spAccessView").toggle(view === "access");
  1213. });
  1214. $("#spAddDrive").on("click", openAddDrive);
  1215. $("#spPanelClose").on("click", closePanel);
  1216. $("#spPanelBack").on("mousedown", function (event) {
  1217. if (event.target === this) closePanel();
  1218. });
  1219. $("#spReload").on("click", reloadPools);
  1220. $("#spNarrowGroup").on("change", renderNarrowList);
  1221. $("#spCheckButton").on("click", checkUser);
  1222. $("#spCheckUser").on("keydown", function (event) {
  1223. if (event.key === "Enter") checkUser();
  1224. });
  1225. function onDocumentClick(event) {
  1226. if ($(event.target).closest(".sp-menu-wrap").length === 0) closeMenus();
  1227. }
  1228. function onDocumentKey(event) {
  1229. if (event.key !== "Escape") return;
  1230. closeMenus();
  1231. if ($("#spPanelBack").hasClass("open")) closePanel();
  1232. }
  1233. $(document).on("click", onDocumentClick).on("keydown", onDocumentKey);
  1234. window.ssOnPageLeave = function () {
  1235. $(document).off("click", onDocumentClick).off("keydown", onDocumentKey);
  1236. };
  1237. spLocale.init("../locale/disk/storagepool.json", function () {
  1238. spLocale.translate();
  1239. $("#spCheckUser").attr("placeholder", T("sp/check/user", "Username"));
  1240. loadDrives();
  1241. });
  1242. })();
  1243. </script>