elFinderSupportVer1.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. "use strict";
  2. /**
  3. * elFinder transport to support old protocol.
  4. *
  5. * @example
  6. * $('selector').elfinder({
  7. * ....
  8. * transport : new elFinderSupportVer1()
  9. * })
  10. *
  11. * @author Dmitry (dio) Levashov
  12. **/
  13. window.elFinderSupportVer1 = function(upload) {
  14. var self = this;
  15. this.upload = upload || 'auto';
  16. this.init = function(fm) {
  17. this.fm = fm;
  18. this.fm.parseUploadData = function(text) {
  19. var data;
  20. if (!$.trim(text)) {
  21. return {error : ['errResponse', 'errDataEmpty']};
  22. }
  23. try {
  24. data = $.parseJSON(text);
  25. } catch (e) {
  26. return {error : ['errResponse', 'errDataNotJSON']}
  27. }
  28. return self.normalize('upload', data);
  29. }
  30. }
  31. this.send = function(opts) {
  32. var self = this,
  33. fm = this.fm,
  34. dfrd = $.Deferred(),
  35. cmd = opts.data.cmd,
  36. args = [],
  37. _opts = {},
  38. data,
  39. xhr;
  40. dfrd.abort = function() {
  41. xhr.state() == 'pending' && xhr.abort();
  42. }
  43. switch (cmd) {
  44. case 'open':
  45. opts.data.tree = 1;
  46. break;
  47. case 'parents':
  48. case 'tree':
  49. return dfrd.resolve({tree : []});
  50. break;
  51. case 'get':
  52. opts.data.cmd = 'read';
  53. opts.data.current = fm.file(opts.data.target).phash;
  54. break;
  55. case 'put':
  56. opts.data.cmd = 'edit';
  57. opts.data.current = fm.file(opts.data.target).phash;
  58. break;
  59. case 'archive':
  60. case 'rm':
  61. opts.data.current = fm.file(opts.data.targets[0]).phash;
  62. break;
  63. case 'extract':
  64. case 'rename':
  65. case 'resize':
  66. opts.data.current = fm.file(opts.data.target).phash;
  67. break;
  68. case 'duplicate':
  69. _opts = $.extend(true, {}, opts);
  70. $.each(opts.data.targets, function(i, hash) {
  71. $.ajax($.extend(_opts, {data : {cmd : 'duplicate', target : hash, current : fm.file(hash).phash}}))
  72. .error(function(error) {
  73. fm.error(fm.res('error', 'connect'));
  74. })
  75. .done(function(data) {
  76. data = self.normalize('duplicate', data);
  77. if (data.error) {
  78. fm.error(data.error);
  79. } else if (data.added) {
  80. fm.trigger('add', {added : data.added});
  81. }
  82. })
  83. });
  84. return dfrd.resolve({})
  85. break;
  86. case 'mkdir':
  87. case 'mkfile':
  88. opts.data.current = opts.data.target;
  89. break;
  90. case 'paste':
  91. opts.data.current = opts.data.dst
  92. break;
  93. case 'size':
  94. return dfrd.resolve({error : fm.res('error', 'cmdsupport')});
  95. break;
  96. case 'search':
  97. return dfrd.resolve({error : fm.res('error', 'cmdsupport')});
  98. break;
  99. }
  100. // cmd = opts.data.cmd
  101. xhr = $.ajax(opts)
  102. .fail(function(error) {
  103. dfrd.reject(error)
  104. })
  105. .done(function(raw) {
  106. data = self.normalize(cmd, raw);
  107. // cmd != 'open' && self.fm.log(data);
  108. if (cmd == 'paste' && !data.error) {
  109. fm.sync();
  110. dfrd.resolve({});
  111. } else {
  112. dfrd.resolve(data);
  113. }
  114. })
  115. return dfrd;
  116. return $.ajax(opts);
  117. }
  118. // fix old connectors errors messages as possible
  119. // this.errors = {
  120. // 'Unknown command' : 'Unknown command.',
  121. // 'Invalid backend configuration' : 'Invalid backend configuration.',
  122. // 'Access denied' : 'Access denied.',
  123. // 'PHP JSON module not installed' : 'PHP JSON module not installed.',
  124. // 'File not found' : 'File not found.',
  125. // 'Invalid name' : 'Invalid file name.',
  126. // 'File or folder with the same name already exists' : 'File named "$1" already exists in this location.',
  127. // 'Not allowed file type' : 'Not allowed file type.',
  128. // 'File exceeds the maximum allowed filesize' : 'File exceeds maximum allowed size.',
  129. // 'Unable to copy into itself' : 'Unable to copy "$1" into itself.',
  130. // 'Unable to create archive' : 'Unable to create archive.',
  131. // 'Unable to extract files from archive' : 'Unable to extract files from "$1".'
  132. // }
  133. this.normalize = function(cmd, data) {
  134. var self = this,
  135. files = {},
  136. filter = function(file) { return file && file.hash && file.name && file.mime ? file : null; },
  137. phash;
  138. if ((cmd == 'tmb' || cmd == 'get')) {
  139. return data;
  140. }
  141. // if (data.error) {
  142. // $.each(data.error, function(i, msg) {
  143. // if (self.errors[msg]) {
  144. // data.error[i] = self.errors[msg];
  145. // }
  146. // });
  147. // }
  148. if (cmd == 'upload' && data.error && data.cwd) {
  149. data.warning = $.extend({}, data.error);
  150. data.error = false;
  151. }
  152. if (data.error) {
  153. return data;
  154. }
  155. if (cmd == 'put') {
  156. phash = this.fm.file(data.target.hash).phash;
  157. return {changed : [this.normalizeFile(data.target, phash)]};
  158. }
  159. phash = data.cwd.hash;
  160. if (data.tree) {
  161. $.each(this.normalizeTree(data.tree), function(i, file) {
  162. files[file.hash] = file;
  163. });
  164. }
  165. $.each(data.cdc||[], function(i, file) {
  166. var hash = file.hash;
  167. if (files[hash]) {
  168. files[hash].date = file.date;
  169. files[hash].locked = file.hash == phash ? true : file.rm === void(0) ? false : !file.rm;
  170. } else {
  171. files[hash] = self.normalizeFile(file, phash, data.tmb);
  172. }
  173. });
  174. if (!data.tree) {
  175. $.each(this.fm.files(), function(hash, file) {
  176. if (!files[hash] && file.phash != phash && file.mime == 'directory') {
  177. files[hash] = file;
  178. }
  179. });
  180. }
  181. if (cmd == 'open') {
  182. return {
  183. cwd : files[phash] || this.normalizeFile(data.cwd),
  184. files : $.map(files, function(f) { return f }),
  185. options : self.normalizeOptions(data),
  186. init : !!data.params,
  187. debug : data.debug
  188. };
  189. }
  190. return $.extend({
  191. current : data.cwd.hash,
  192. error : data.error,
  193. warning : data.warning,
  194. options : {tmb : !!data.tmb}
  195. }, this.fm.diff($.map(files, filter)));
  196. }
  197. /**
  198. * Convert old api tree into plain array of dirs
  199. *
  200. * @param Object root dir
  201. * @return Array
  202. */
  203. this.normalizeTree = function(root) {
  204. var self = this,
  205. result = [],
  206. traverse = function(dirs, phash) {
  207. var i, dir;
  208. for (i = 0; i < dirs.length; i++) {
  209. dir = dirs[i];
  210. result.push(self.normalizeFile(dir, phash))
  211. dir.dirs.length && traverse(dir.dirs, dir.hash);
  212. }
  213. };
  214. traverse([root]);
  215. return result;
  216. }
  217. /**
  218. * Convert file info from old api format into new one
  219. *
  220. * @param Object file
  221. * @param String parent dir hash
  222. * @return Object
  223. */
  224. this.normalizeFile = function(file, phash, tmb) {
  225. var mime = file.mime || 'directory',
  226. size = mime == 'directory' && !file.linkTo ? 0 : file.size,
  227. info = {
  228. url : file.url,
  229. hash : file.hash,
  230. phash : phash,
  231. name : file.name,
  232. mime : mime,
  233. date : file.date || 'unknown',
  234. size : size,
  235. read : file.read,
  236. write : file.write,
  237. locked : !phash ? true : file.rm === void(0) ? false : !file.rm
  238. };
  239. if (file.mime == 'application/x-empty') {
  240. info.mime = 'text/plain';
  241. }
  242. if (file.linkTo) {
  243. info.alias = file.linkTo;
  244. }
  245. if (file.linkTo) {
  246. info.linkTo = file.linkTo;
  247. }
  248. if (file.tmb) {
  249. info.tmb = file.tmb;
  250. } else if (info.mime.indexOf('image/') === 0 && tmb) {
  251. info.tmb = 1;
  252. }
  253. if (file.dirs && file.dirs.length) {
  254. info.dirs = true;
  255. }
  256. if (file.dim) {
  257. info.dim = file.dim;
  258. }
  259. if (file.resize) {
  260. info.resize = file.resize;
  261. }
  262. return info;
  263. }
  264. this.normalizeOptions = function(data) {
  265. var opts = {
  266. path : data.cwd.rel,
  267. disabled : data.disabled || [],
  268. tmb : !!data.tmb,
  269. copyOverwrite : true
  270. };
  271. if (data.params) {
  272. opts.api = 1;
  273. opts.url = data.params.url;
  274. opts.archivers = {
  275. create : data.params.archives || [],
  276. extract : data.params.extract || []
  277. }
  278. }
  279. if (opts.path.indexOf('/') !== -1) {
  280. opts.separator = '/';
  281. } else if (opts.path.indexOf('\\') !== -1) {
  282. opts.separator = '\\';
  283. }
  284. return opts;
  285. }
  286. }