shared.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. shared.js
  3. "Shared with me": the files and folders other users shared with this one,
  4. browsed as the share:/ drive instead of one share link page at a time.
  5. The drive is a real directory tree as far as the File Manager is
  6. concerned - the server lists, searches, opens and copies it like any other
  7. (mod/filesystem/abstractions/sharefs). What it adds here is knowing what
  8. each shared item allows, so only the operations that can work are offered:
  9. /system/file_system/share/sharedWithMe -> [SharedItemInfo]
  10. lists the items at the drive root with their permissions. Every path on
  11. the drive starts with the item it belongs to ("share:/Photos/2024/a.jpg"
  12. is inside "Photos"), so one lookup by the first segment answers for any
  13. file below it.
  14. Nothing on the drive can be renamed, deleted, moved, uploaded into or
  15. shared again: the files belong to their owners, and the server refuses all
  16. of it (the drive is read only). Copying and downloading follow the share's
  17. "allow download" setting. Uploading into a folder shared for editing, and
  18. downloading a folder whose content the owner hid, still happen on the
  19. share link page, which "Open share page" brings up.
  20. Part of the ArozOS File Manager. Loaded as a plain script from
  21. file_explorer.html - see the <script> block at the end of that file.
  22. Loaded before trash.js so its sidebar entry sits above the trash bin.
  23. */
  24. //The sentinel the sidebar entry and desktop shortcut hold, and the drive it names
  25. const SHARE_VPATH = "%share%";
  26. const SHARE_ROOT = "share:/";
  27. /*
  28. Item name -> SharedItemInfo from the last listing. null until the first
  29. one arrives; the menus then allow only what never depends on an item.
  30. */
  31. let sharedItems = null;
  32. let sharedItemsRequest = null;
  33. function isSharePath(path){
  34. return getDriveView(path) === getSpecialView(SHARE_VPATH);
  35. }
  36. //The name of the shared item a path on the drive belongs to, "" for the root
  37. function sharedItemNameOf(path){
  38. let rest = String(path).trim().substring(SHARE_ROOT.length);
  39. return rest.split("/")[0];
  40. }
  41. function sharedItemOf(path){
  42. if (sharedItems == null || !isSharePath(path)){
  43. return null;
  44. }
  45. let item = sharedItems[sharedItemNameOf(path)];
  46. return item == undefined ? null : item;
  47. }
  48. /*
  49. Fetch what each item allows. Concurrent callers share one request; the
  50. callback runs either way, with whatever is known by then.
  51. */
  52. function loadSharedItems(callback){
  53. if (sharedItemsRequest == null){
  54. sharedItemsRequest = $.get("../../system/file_system/share/sharedWithMe").done(function(data){
  55. if (Array.isArray(data)){
  56. let items = {};
  57. data.forEach(function(item){
  58. items[item.Name] = item;
  59. });
  60. sharedItems = items;
  61. }
  62. }).always(function(){
  63. sharedItemsRequest = null;
  64. });
  65. }
  66. sharedItemsRequest.always(function(){
  67. if (typeof callback === "function"){
  68. callback();
  69. }
  70. });
  71. }
  72. /*
  73. The rules. paths are the selected files, or empty for the directory
  74. itself (right click on empty space, the toolbar with nothing selected).
  75. */
  76. function sharedDriveAllows(opr, paths){
  77. let targets = (paths != undefined && paths.length > 0) ? paths : [currentPath];
  78. let items = targets.map(sharedItemOf);
  79. let allItems = items.length > 0 && items.every(function(item){ return item != null; });
  80. switch (opr){
  81. //Looking around works everywhere on the drive
  82. case "open":
  83. case "openwith":
  84. case "refresh":
  85. case "home":
  86. case "fileinfo":
  87. case "dirinfo":
  88. case "shortcut":
  89. return true;
  90. //Taking a copy is the owner's call, item by item
  91. case "copy":
  92. case "download":
  93. return allItems && items.every(function(item){ return item.CanDownload; });
  94. //The link page of one item: its zip download and uploads live there
  95. case "sharepage":
  96. return allItems && items.length == 1 && items[0].ShareUUID != "";
  97. }
  98. //cut, paste, rename, delete, upload, newfile, newfolder, zip, unzip, share
  99. return false;
  100. }
  101. //Why sharedDriveAllows() said no, for driveViewBlocks()
  102. function sharedDriveRefusal(opr){
  103. if (opr == "copy" || opr == "download"){
  104. let item = sharedItemOf(currentPath);
  105. if (item == null && $(".fileObject.selected").length > 0){
  106. item = sharedItemOf($(".fileObject.selected").first().attr("filepath"));
  107. }
  108. if (item != null && !item.CanDownload){
  109. return applocale.getString("message/share/noDownload",
  110. "The owner of this item does not allow downloading it");
  111. }
  112. }
  113. return applocale.getString("message/share/readOnly",
  114. "Files shared with you are read only");
  115. }
  116. /*
  117. Open the share link page of the selected item (or of the folder on screen),
  118. where a folder shared for editing takes uploads and one whose listing is
  119. hidden can still be downloaded as a zip.
  120. */
  121. function openSharedItemPage(){
  122. let path = currentPath;
  123. if ($(".fileObject.selected").length > 0){
  124. path = $(".fileObject.selected").first().attr("filepath");
  125. }
  126. let item = sharedItemOf(path);
  127. if (item == null || item.ShareUUID == ""){
  128. return;
  129. }
  130. //A float window on the desktop, a new tab otherwise
  131. ao_module_newfw({
  132. url: "share/" + encodeURIComponent(item.ShareUUID),
  133. width: 1080,
  134. height: 640,
  135. appicon: "SystemAO/file_system/img/share.svg",
  136. title: item.Name
  137. });
  138. }
  139. /*
  140. Shown instead of a listing when a folder's owner hid its content. The
  141. server refuses the listing either way; this says why and offers the link
  142. page, which can still download the folder as a zip.
  143. */
  144. function renderSharedFolderHidden(item){
  145. $("#fileList").hide();
  146. let html = '<div class="fmPathError">' +
  147. '<div class="fmPathErrorIcon">' + FSIcons.sharedWithMe + '</div>' +
  148. '<div class="fmPathErrorTitle">' + escapeHTMLText(applocale.getString("share/hidden/title", "This folder's content is hidden")) + '</div>' +
  149. '<div class="fmPathErrorDesc">' + escapeHTMLText(applocale.getString("share/hidden/desc",
  150. "{owner} shared this folder without showing what is inside. It may still be downloaded from its share page.").replace("{owner}", item.Owner)) + '</div>' +
  151. '<div class="fmPathErrorActions">';
  152. if (item.ShareUUID != ""){
  153. html += '<button class="fmPathErrorBtn primary" onclick="openSharedItemPage();">' +
  154. escapeHTMLText(applocale.getString("contextmenu/sharepage", "Open Share Page")) + '</button>';
  155. }
  156. html += '<button class="fmPathErrorBtn" onclick="listDirectory(SHARE_ROOT);">' +
  157. escapeHTMLText(applocale.getString("share/title", "Shared with me")) + '</button>' +
  158. '</div></div>';
  159. $("#folderList").show().html(html);
  160. //Nothing is listed, so the status bar should not count the last folder
  161. updateSelectedObjectsCount();
  162. }
  163. //An empty drive root says what would appear there, rather than nothing at all
  164. function renderSharedEmpty(path){
  165. if (sharedItemNameOf(path) != ""){
  166. return;
  167. }
  168. $("#fileList").hide();
  169. $("#folderList").show().html('<div class="fmPathError">' +
  170. '<div class="fmPathErrorIcon">' + FSIcons.sharedWithMe + '</div>' +
  171. '<div class="fmPathErrorTitle">' + escapeHTMLText(applocale.getString("share/empty/title", "Nothing is shared with you yet")) + '</div>' +
  172. '<div class="fmPathErrorDesc">' + escapeHTMLText(applocale.getString("share/empty/desc",
  173. "Files and folders other users share with you, or with a group you are in, appear here.")) + '</div>' +
  174. '</div>');
  175. }
  176. /*
  177. Called by listDirectory() for every path on the drive, before the listing
  178. goes out. Refreshes what the items allow - a share can change while the
  179. window is open - and reports whether the listing should go ahead.
  180. */
  181. function prepareSharedListing(path, proceed){
  182. loadSharedItems(function(){
  183. if (currentPath != path){
  184. //The user moved on while this was loading
  185. return;
  186. }
  187. let item = sharedItemOf(path);
  188. if (item != null && item.IsFolder && !item.CanBrowse){
  189. renderSharedFolderHidden(item);
  190. applyDriveViewToolbar();
  191. return;
  192. }
  193. applyDriveViewToolbar();
  194. proceed();
  195. });
  196. }
  197. /*
  198. Registration
  199. Before trash.js registers the trash bin, so this entry comes first in the
  200. sidebar - right above the bin.
  201. */
  202. registerSpecialView(SHARE_VPATH, {
  203. icon: "sharedWithMe",
  204. labelKey: "share/title",
  205. labelFallback: "Shared with me",
  206. sidebar: true,
  207. root: SHARE_ROOT,
  208. allows: sharedDriveAllows,
  209. refusal: sharedDriveRefusal,
  210. prepare: prepareSharedListing,
  211. empty: renderSharedEmpty
  212. });
  213. /*
  214. Reachable from outside this file: inline on* attributes in the markup,
  215. handlers generated in template strings, or another frame. Renaming any
  216. of these means updating those call sites too.
  217. */
  218. window.openSharedItemPage = openSharedItemPage;
  219. window.isSharePath = isSharePath;