File indexing completed on 2024-12-22 05:35:31
0001 "use strict"; 0002 /** 0003 * elFinder transport to support old protocol. 0004 * 0005 * @example 0006 * $('selector').elfinder({ 0007 * .... 0008 * transport : new elFinderSupportVer1() 0009 * }) 0010 * 0011 * @author Dmitry (dio) Levashov 0012 **/ 0013 window.elFinderSupportVer1 = function(upload) { 0014 var self = this; 0015 0016 this.upload = upload || 'auto'; 0017 0018 this.init = function(fm) { 0019 this.fm = fm; 0020 this.fm.parseUploadData = function(text) { 0021 var data; 0022 0023 if (!$.trim(text)) { 0024 return {error : ['errResponse', 'errDataEmpty']}; 0025 } 0026 0027 try { 0028 data = $.parseJSON(text); 0029 } catch (e) { 0030 return {error : ['errResponse', 'errDataNotJSON']} 0031 } 0032 0033 return self.normalize('upload', data); 0034 } 0035 } 0036 0037 0038 this.send = function(opts) { 0039 var self = this, 0040 fm = this.fm, 0041 dfrd = $.Deferred(), 0042 cmd = opts.data.cmd, 0043 args = [], 0044 _opts = {}, 0045 data, 0046 xhr; 0047 0048 dfrd.abort = function() { 0049 !xhr.isRejected() && !xhr.isResolved() && xhr.abort(); 0050 } 0051 0052 switch (cmd) { 0053 case 'open': 0054 opts.data.tree = 1; 0055 break; 0056 case 'parents': 0057 case 'tree': 0058 return dfrd.resolve({tree : []}); 0059 break; 0060 case 'get': 0061 opts.data.cmd = 'read'; 0062 opts.data.current = fm.file(opts.data.target).phash; 0063 break; 0064 case 'put': 0065 opts.data.cmd = 'edit'; 0066 opts.data.current = fm.file(opts.data.target).phash; 0067 break; 0068 case 'archive': 0069 case 'rm': 0070 opts.data.current = fm.file(opts.data.targets[0]).phash; 0071 break; 0072 case 'extract': 0073 case 'rename': 0074 case 'resize': 0075 opts.data.current = fm.file(opts.data.target).phash; 0076 break; 0077 case 'duplicate': 0078 _opts = $.extend(true, {}, opts); 0079 0080 $.each(opts.data.targets, function(i, hash) { 0081 $.ajax($.extend(_opts, {data : {cmd : 'duplicate', target : hash, current : fm.file(hash).phash}})) 0082 .error(function(error) { 0083 fm.error(fm.res('error', 'connect')); 0084 }) 0085 .done(function(data) { 0086 data = self.normalize('duplicate', data); 0087 if (data.error) { 0088 fm.error(data.error); 0089 } else if (data.added) { 0090 fm.trigger('add', {added : data.added}); 0091 } 0092 }) 0093 }); 0094 return dfrd.resolve({}) 0095 break; 0096 0097 case 'mkdir': 0098 case 'mkfile': 0099 opts.data.current = opts.data.target; 0100 break; 0101 case 'paste': 0102 opts.data.current = opts.data.dst 0103 break; 0104 0105 case 'size': 0106 return dfrd.resolve({error : fm.res('error', 'cmdsupport')}); 0107 break; 0108 case 'search': 0109 return dfrd.resolve({error : fm.res('error', 'cmdsupport')}); 0110 break; 0111 0112 } 0113 // cmd = opts.data.cmd 0114 0115 xhr = $.ajax(opts) 0116 .fail(function(error) { 0117 dfrd.reject(error) 0118 }) 0119 .done(function(raw) { 0120 data = self.normalize(cmd, raw); 0121 0122 // cmd != 'open' && self.fm.log(data); 0123 0124 if (cmd == 'paste' && !data.error) { 0125 fm.sync(); 0126 dfrd.resolve({}); 0127 } else { 0128 dfrd.resolve(data); 0129 } 0130 }) 0131 0132 return dfrd; 0133 0134 return $.ajax(opts); 0135 } 0136 0137 // fix old connectors errors messages as possible 0138 // this.errors = { 0139 // 'Unknown command' : 'Unknown command.', 0140 // 'Invalid backend configuration' : 'Invalid backend configuration.', 0141 // 'Access denied' : 'Access denied.', 0142 // 'PHP JSON module not installed' : 'PHP JSON module not installed.', 0143 // 'File not found' : 'File not found.', 0144 // 'Invalid name' : 'Invalid file name.', 0145 // 'File or folder with the same name already exists' : 'File named "$1" already exists in this location.', 0146 // 'Not allowed file type' : 'Not allowed file type.', 0147 // 'File exceeds the maximum allowed filesize' : 'File exceeds maximum allowed size.', 0148 // 'Unable to copy into itself' : 'Unable to copy "$1" into itself.', 0149 // 'Unable to create archive' : 'Unable to create archive.', 0150 // 'Unable to extract files from archive' : 'Unable to extract files from "$1".' 0151 // } 0152 0153 this.normalize = function(cmd, data) { 0154 var self = this, 0155 files = {}, 0156 filter = function(file) { return file && file.hash && file.name && file.mime ? file : null; }, 0157 phash; 0158 0159 if ((cmd == 'tmb' || cmd == 'get')) { 0160 return data; 0161 } 0162 0163 // if (data.error) { 0164 // $.each(data.error, function(i, msg) { 0165 // if (self.errors[msg]) { 0166 // data.error[i] = self.errors[msg]; 0167 // } 0168 // }); 0169 // } 0170 0171 if (cmd == 'upload' && data.error && data.cwd) { 0172 data.warning = $.extend({}, data.error); 0173 data.error = false; 0174 } 0175 0176 0177 if (data.error) { 0178 return data; 0179 } 0180 0181 if (cmd == 'put') { 0182 0183 phash = this.fm.file(data.target.hash).phash; 0184 return {changed : [this.normalizeFile(data.target, phash)]}; 0185 } 0186 0187 phash = data.cwd.hash; 0188 0189 if (data.tree) { 0190 $.each(this.normalizeTree(data.tree), function(i, file) { 0191 files[file.hash] = file; 0192 }); 0193 } 0194 0195 $.each(data.cdc||[], function(i, file) { 0196 var hash = file.hash; 0197 0198 if (files[hash]) { 0199 files[hash].date = file.date; 0200 files[hash].locked = file.hash == phash ? true : file.rm === void(0) ? false : !file.rm; 0201 } else { 0202 files[hash] = self.normalizeFile(file, phash, data.tmb); 0203 } 0204 }); 0205 0206 if (!data.tree) { 0207 $.each(this.fm.files(), function(hash, file) { 0208 if (!files[hash] && file.phash != phash && file.mime == 'directory') { 0209 files[hash] = file; 0210 } 0211 }); 0212 } 0213 0214 if (cmd == 'open') { 0215 return { 0216 cwd : files[phash] || this.normalizeFile(data.cwd), 0217 files : $.map(files, function(f) { return f }), 0218 options : self.normalizeOptions(data), 0219 init : !!data.params, 0220 debug : data.debug 0221 }; 0222 } 0223 0224 0225 0226 return $.extend({ 0227 current : data.cwd.hash, 0228 error : data.error, 0229 warning : data.warning, 0230 options : {tmb : !!data.tmb} 0231 }, this.fm.diff($.map(files, filter))); 0232 0233 } 0234 0235 /** 0236 * Convert old api tree into plain array of dirs 0237 * 0238 * @param Object root dir 0239 * @return Array 0240 */ 0241 this.normalizeTree = function(root) { 0242 var self = this, 0243 result = [], 0244 traverse = function(dirs, phash) { 0245 var i, dir; 0246 0247 for (i = 0; i < dirs.length; i++) { 0248 dir = dirs[i]; 0249 result.push(self.normalizeFile(dir, phash)) 0250 dir.dirs.length && traverse(dir.dirs, dir.hash); 0251 } 0252 }; 0253 0254 traverse([root]); 0255 0256 return result; 0257 } 0258 0259 /** 0260 * Convert file info from old api format into new one 0261 * 0262 * @param Object file 0263 * @param String parent dir hash 0264 * @return Object 0265 */ 0266 this.normalizeFile = function(file, phash, tmb) { 0267 var mime = file.mime || 'directory', 0268 size = mime == 'directory' && !file.linkTo ? 0 : file.size, 0269 info = { 0270 url : file.url, 0271 hash : file.hash, 0272 phash : phash, 0273 name : file.name, 0274 mime : mime, 0275 date : file.date || 'unknown', 0276 size : size, 0277 read : file.read, 0278 write : file.write, 0279 locked : !phash ? true : file.rm === void(0) ? false : !file.rm 0280 }; 0281 0282 if (file.mime == 'application/x-empty') { 0283 info.mime = 'text/plain'; 0284 } 0285 if (file.linkTo) { 0286 info.alias = file.linkTo; 0287 } 0288 0289 if (file.linkTo) { 0290 info.linkTo = file.linkTo; 0291 } 0292 0293 if (file.tmb) { 0294 info.tmb = file.tmb; 0295 } else if (info.mime.indexOf('image/') === 0 && tmb) { 0296 info.tmb = 1; 0297 0298 } 0299 0300 if (file.dirs && file.dirs.length) { 0301 info.dirs = true; 0302 } 0303 if (file.dim) { 0304 info.dim = file.dim; 0305 } 0306 if (file.resize) { 0307 info.resize = file.resize; 0308 } 0309 return info; 0310 } 0311 0312 this.normalizeOptions = function(data) { 0313 var opts = { 0314 path : data.cwd.rel, 0315 disabled : data.disabled || [], 0316 tmb : !!data.tmb, 0317 copyOverwrite : true 0318 }; 0319 0320 if (data.params) { 0321 opts.api = 1; 0322 opts.url = data.params.url; 0323 opts.archivers = { 0324 create : data.params.archives || [], 0325 extract : data.params.extract || [] 0326 } 0327 } 0328 0329 if (opts.path.indexOf('/') !== -1) { 0330 opts.separator = '/'; 0331 } else if (opts.path.indexOf('\\') !== -1) { 0332 opts.separator = '\\'; 0333 } 0334 return opts; 0335 } 0336 0337 0338 }