Dateiverwaltung für die WebBox
ulrich
2021-01-06 53119fbeeaf9dbbf6c3a2ae492b9940cb5550054
commit | author | age
2f9e6f 1 /*
U 2     Dateiverwaltung - File management in your browser
3     Copyright (C) 2017 Ulrich Hilger, http://uhilger.de
4
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU Affero General Public License as
7     published by the Free Software Foundation, either version 3 of the
8     License, or (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU Affero General Public License for more details.
14
15     You should have received a copy of the GNU Affero General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20
21 /*
22  * In api.js finden sich die HTTP-Aufrufe vom Browser-Client 
23  * der Dateiverwaltung an deren Server-Kern
24  */
25
26
27
28
29 /* ---------- Dateien kopieren, verschieben --------------- */
30
31 /*
32  * Hier wird fuer eine zuvor markierte Liste von Dateien, fuer die 
33  * Cut oder Copy gewaehlt wurde, die Operations ausgefuehrt (move oder copy)
34  * @returns {undefined}
35  */
36 function fm_paste_files() {
37   var m;
38   if(cutCopyOperation === 'cut') {
39     //m = '?c=de.uhilger.filecms.api.FileMgr&m=moveFiles'; //&p=' + cutCopySrcDir + '&p=' + pfad + '&p=' + encodeURIComponent(liste);
40     m = '?c=de.uhilger.filecms.api.FileMgr&m=moveFiles&p=' + cutCopySrcDir + '&p=' + pfad + '&p=' + encodeURIComponent(cutCopyFiles);
41   } else {
42     //m = '?c=de.uhilger.filecms.api.FileMgr&m=copyFiles'; //&p=' + cutCopySrcDir + '&p=' + pfad + '&p=' + encodeURIComponent(liste);
43     m = '?c=de.uhilger.filecms.api.FileMgr&m=copyFiles&p=' + cutCopySrcDir + '&p=' + pfad + '&p=' + encodeURIComponent(cutCopyFiles);
44   }
45   var u = '../svc' + m;  
46   fm_get(u, "text", function(resp) {
47     // console.log('deleteFiles gab folgendes zurueck: ' + resp);
48     fm_get_list(pfad);
49   });
50   /*
51   fm_post(u, {p1: encodeURIComponent(cutCopySrcDir), p2: encodeURIComponent(pfad), p3: encodeURIComponent(cutCopyFiles)},'text', function(resp) {
52     // resp evtl. zeigen..
53     fm_get_list(pfad);
54   });
55   */
56 }
57
58 /* ---------- Dateien listen, loeschen, umbenennen --------------- */
59
60 // http://localhost:8079/file-cms/svc?c=de.uhilger.filecms.api.FileMgr&f=JSONNICE&m=list&p=
53119f 61
U 62 /*
63  * standardmaessig aufgerufen von fm_dateiwahl, immer mit dem Inhalt von pfad
64  * 
65  * relPfad Persoenlich, Oeffentlich oder Austausch als Startwert
66  * dann der relative Pfad aus den angeklickten Ordnernamen
67  * 
68  * @param {type} relPfad
69  * @returns {undefined}
70  */
2f9e6f 71 function fm_get_list(relPfad) {
U 72   $('#ansicht').attr('onclick','').unbind('click');
73   var m = '?c=de.uhilger.filecms.api.FileMgr&m=list&p=' + relPfad;
74   var u = '../svc' + m;
75   fm_get(u, "json", function(resp) {
76     
77     if(resp.List[0].FileRef !== undefined) {
78       var files = new Array();
79       if(resp.List[0].FileRef instanceof Array) {
80         for(var i = 0; i < resp.List[0].FileRef.length; i++) {
81           files.push(new FileRef(resp.List[0].FileRef[i]));
82         }
83       } else {
84         files.push(new FileRef(resp.List[0].FileRef));
85       }
86       var fl = new FileList(files);
87       fm_render_list(fl);
88     } else {
89       $('#dateien').empty();
90     }
91     
92     // Breadcrumb-Ansicht
93     var template;
94     $('.breadcrumb-item').attr('onclick','').unbind('click');
95     $('#bcnav').empty();
96     var dirList = new Array();
97     var rp = '';
98     //console.log("'" + relPfad + "'");
99     var dirs = relPfad.split('/');
100     //console.log(dirs.length);
101     dirList.push(new BcrFile(rp, 'Home'));
102     if(relPfad.length > 0) {
103       for(var i = 0; i < dirs.length - 1; i++) {
104         if(rp.length > 0 ) {
105           dirList.push(new BcrFile(rp + '/' + dirs[i], dirs[i]));
106           rp = rp + '/' + dirs[i];
107         } else {
108           dirList.push(new BcrFile(dirs[i], dirs[i]));
109           rp = dirs[i];
110         }
111       }
112       var bl = new BcrFiles(dirList);
113  
114       if(dirList.length > 0) {
115         template = $('#tpl-bcr').html();
116         Mustache.parse(template);   // optional, speeds up future uses
117         $('#bcnav').html(Mustache.render(template, bl));
118       }
119       
120       if(dirs.length > 0) {
121         dirList.push(new BcrFile(rp + '/' + dirs[dirs.length-1], dirs[dirs.length-1]));
122         template = $('#tpl-bcr2').html();
123         Mustache.parse(template);   // optional, speeds up future uses
124         $('#bcnav').append(Mustache.render(template, dirList[dirList.length-1]));        
125       } else {
126         template = $('#tpl-bcr2').html();
127         Mustache.parse(template);   // optional, speeds up future uses
128         $('#bcnav').append(Mustache.render(template, dirList[0]));        
129       }
130       
131       $('#bcnav').append($('#tpl-bcr3').html());
132     } else {
133       pfad = '';
134       template = $('#tpl-bcr2').html();
135       Mustache.parse(template);   // optional, speeds up future uses
136       $('#bcnav').append(Mustache.render(template, dirList[0]));   
137       $('#bcnav').append($('#tpl-bcr3').html());
138     }
139     $('.breadcrumb-item').click(fm_bc_click);
140     $('#ansicht').click(fm_ansicht_umschalten);
141     fm_set_modus();
630c3f 142     
76660f 143     if(fm_slideshow) {
U 144       $("[data-fancybox]").fancybox({
145         loop: true,
146         idleTime: 2
147       });
148     }
2f9e6f 149   });
U 150 }
151
152 function fm_del_files() {
153   var liste = fm_gewaehlte_dateien();
154   var m = '?c=de.uhilger.filecms.api.FileMgr&m=deleteFiles&p=' + pfad + '&p=' + encodeURIComponent(liste);
155   var u = '../svc' + m;
156   fm_get(u, "text", function(resp) {
157     // console.log('deleteFiles gab folgendes zurueck: ' + resp);
158     fm_get_list(pfad);
159   });
160 }
161
af9930 162 function fm_pack_folder() {
U 163   //var liste = fm_gewaehlte_dateien();
164   var m = '?c=de.uhilger.filecms.api.FileMgr&m=packFolder&p=' + pfad;
165   var u = '../svc' + m;
166   fm_get(u, "text", function(resp) {
167     // console.log('deleteFiles gab folgendes zurueck: ' + resp);
168     fm_get_list(pfad);
169   });
170 }
171
2f9e6f 172 function fm_rename_file(fn, p, neuerName) {
U 173   var m = '?c=de.uhilger.filecms.api.FileMgr&m=renameFile';
174   m = m + '&p=' + p; 
175   m = m + '&p=' + fn; 
176   m = m + '&p=' + neuerName; 
177   var u = '../svc' + m;
178   fm_get(u, "text", function(resp) {
179     fm_get_list(pfad);
180   });  
181 }
182
183 /* -------------------- Ordner ---------------- */
184
185 function fm_get_new_folder() {
186   $('#modal_ok').click(function() {
187     // hier speichern
188     var m = '?c=de.uhilger.filecms.api.FileMgr&m=newFolder&p=' + pfad + '&p=' + $('#dateiname').val();
189     var u = '../svc' + m;
190     fm_get(u, "json", function(resp) {
191       fm_get_list(pfad);
192     });
193   });
194   $('#saveModalTitle').text('Neuer Ordner');
195   $('#dialogfrage').text("Name?");
196   $('#dateiname').val('');
197   $('#dateiname').attr('placeholder', 'Ordnername');
198   $('#saveModal').modal({
199     keyboard: false,
200     show: true
201   });
202 }
203
204 function fm_unzip_file(fn) {
205   var m = '?c=de.uhilger.filecms.api.FileMgr&m=extractZipfile';
206   m = m + '&p=' + pfad; 
207   m = m + '&p=' + fn; 
208   var u = '../svc' + m;
209   fm_get(u, "text", function(resp) {
210     $('.system-out').empty();
211     $('.system-out').append('Rueckmeldung vom Entpacken: ' + resp);
212     fm_fusszeile_zeigen();
213     fm_get_list(pfad);
214   });  
215 }
216
fad719 217 function fm_export_html() {
U 218   var m = '?c=de.uhilger.filecms.api.HtmlExportService&m=exportHtml';
219   m = m + '&p=' + pfad; 
220   var u = '../svc' + m;
221   fm_get(u, "text", function(resp) {
222     $('.system-out').empty();
223     $('.system-out').append('Rueckmeldung vom HTML-Export: ' + resp);
224     fm_fusszeile_zeigen();
225   });  
226 }
227
2f9e6f 228 /* ------------------- Dateiinhalte --------------------- */
U 229
230 function fm_save_file(saveFileName, method, callback) {
231   var content;
232   if(openEditor === 'text') {
233     content = cm.getValue();
234     cm.getDoc().markClean();
235   } else {
236     content = ed.getContent();
237     tinymce.activeEditor.undoManager.clear();
238   }
239   var m = '?c=de.uhilger.filecms.api.FileMgr&m=' + method;
240   var u = '../svc' + m;
241   fm_post(u, {p1: pfad, p2: saveFileName, p3: content}, function(resp) {
242     // hier scheint nichts zurueckzukommen..
243   });
244   openFileName = saveFileName;
245   if(typeof (callback) !== 'function') {
246     
247   } else {
248     callback();
249   }
250 }
251
252 function fm_get_file_content(typ) {
253   var gewaehlte = $('.datei-gewaehlt');
254   //var fname = $(gewaehlte).find('.dateiname').text();
255   
256   var fname = $(gewaehlte[0]).text();
257   openFileName = fname;
258   var m = '?c=de.uhilger.filecms.api.FileMgr&m=getCode&p=' + pfad + '&p=' + fname;
259   var u = '../svc' + m;
260   fm_get(u, "text", function(resp) {
261     if(typ == 'text') {
262       var mode = "text/x-java";
263       if(fname.endsWith('js')) {
264         mode = 'javascript';
265       } else if(fname.endsWith('xml')) {
266         mode = 'xml';
267       } else if(fname.endsWith('properties')) {
268         mode = 'xml';
d03015 269       } else if(fname.endsWith('adoc')) {
U 270         mode = 'text/x-markdown';
2f9e6f 271       }
U 272       fm_text_edit(resp, mode);
273     } else {
274       fm_dok_edit(resp);
275     }
276   });
277   
278 }
279
280 /* -------- An- und Abmelden ------------- */
281
282 function fm_get_login() {
283   var m = '?c=de.uhilger.filecms.pub.SessionManager&m=getSessionUser';
284   var u = '../pub' + m;
285   fm_get(u, "text", function(resp) {
286     userid = resp;
287     $('#userMenu').text(resp);
288   });  
289 }
290
291 function fm_logout() {
292   var m = '?c=de.uhilger.filecms.pub.SessionManager&m=expireSession';
293   var u = '../pub' + m;
294   
295   fm_get(u, "text", function(resp) {
296     $('#userMenu').text('nicht angemeldet');
297     window.location.href = '../logout.html';
298   });
299 }
300
301 /* -------- Compile / Build ------------- */
302
f59dce 303 function fm_ant_build() {
U 304   var m = '?c=de.uhilger.filecms.api.CompileService&m=antBuild&p=' + pfad;
305   var u = '../svc' + m;
306   fm_get(u, "text", function(resp) {
307     $('.system-out').empty();
308     $('.system-out').append('Ergebnis von Ant build: ' + resp);
309     fm_fusszeile_zeigen();
310   });
311 }
312
2f9e6f 313 function fm_build_app() {
U 314   var m = '?c=de.uhilger.filecms.api.CompileService&m=buildApp&p=' + pfad;
315   var u = '../svc' + m;
316   fm_get(u, "text", function(resp) {
317     $('.system-out').empty();
318     $('.system-out').append('Ergebnis von Build app: ' + resp);
319     fm_fusszeile_zeigen();
320   });
321 }
322
323 function fm_compile_all() {
324   var m = '?c=de.uhilger.filecms.api.CompileService&m=compileAll&p=' + pfad;
325   var u = '../svc' + m;
326   fm_get(u, "json", function(resp) {
327     if(resp.List[0].CompilerIssue !== undefined) {
328       var lno;
329       var eMsg;
330       var issueList = new Array();
331       if(resp.List[0].CompilerIssue instanceof Array) {
332         var issueNo = 0;
333         while(issueNo < resp.List[0].CompilerIssue.length) {
334           /*
335           $('.system-out').append('   +++ ---- +++   ');
336           $('.system-out').append(resp.List[0].CompilerIssue[issueNo].kind);
337           $('.system-out').append(resp.List[0].CompilerIssue[issueNo].lineNumber);
338           $('.system-out').append(resp.List[0].CompilerIssue[issueNo].sourceName);
339           $('.system-out').append(resp.List[0].CompilerIssue[issueNo].message);
340           */
341           var issue = new CompilerIssue(
342             resp.List[0].CompilerIssue[issueNo].sourceName,
343             resp.List[0].CompilerIssue[issueNo].message,
344             resp.List[0].CompilerIssue[issueNo].kind,
345             resp.List[0].CompilerIssue[issueNo].lineNumber
346           );
347           issueList.push(issue);
348
349           /*
350           console.log('   +++ ---- +++   ');
351           console.log(resp.List[0].CompilerIssue[issueNo].kind);
352           console.log(resp.List[0].CompilerIssue[issueNo].lineNumber);
353           console.log(resp.List[0].CompilerIssue[issueNo].sourceName);
354           console.log(resp.List[0].CompilerIssue[issueNo].message);
355           */
356           issueNo++;
357         }
358       } else {
359         //lno = resp.List[0].CompilerIssue.lineNumber;
360         //eMsg = resp.List[0].CompilerIssue.kind + ' ' + resp.List[0].CompilerIssue.message;
361         //$('.system-out').append(lno + ' ' + eMsg);
362         //console.log(lno + ' ' + eMsg);
363         var issue = new CompilerIssue(
364           resp.List[0].CompilerIssue.sourceName,
365           resp.List[0].CompilerIssue.message,
366           resp.List[0].CompilerIssue.kind,
367           resp.List[0].CompilerIssue.lineNumber
368         );
369         issueList.push(issue);        
370       }
371       var theList = new IssueList(issueList);
372       var template = $('#tpl-ci').html();
373       Mustache.parse(template);   // optional, speeds up future uses
374       $('.system-out').empty();
375       $('.system-out').html(Mustache.render(template, theList));
376       $('.sued').show();
377     }
378   });
379 }
380
381 function fm_compile(modeStr, callback) {
382   var liste = fm_gewaehlte_dateien();
3260ed 383   var m = '?c=de.uhilger.filecms.api.CompileService&m=compile';//&p=' + pfad + '&p=' + encodeURIComponent(liste) + 
U 384           //'&p=' + modeStr;
2f9e6f 385   var u = '../svc' + m;
3260ed 386   var data = 'p=' + pfad + '&p=' + encodeURIComponent(liste) + 
U 387           '&p=' + modeStr;
388   fm_post(u, data, "json", function(resp) {
389     callback(resp);
390   });
391   /*
2f9e6f 392   fm_get(u, "json", function(resp) {
U 393     callback(resp);
394   });
3260ed 395   */
2f9e6f 396 }
U 397
398 /* --------------- Bilder ------------------------- */
399
400 function fm_menu_shrink() {
401   var gewaehlte = $('.datei-gewaehlt');
402   var fname = $(gewaehlte[0]).text();
403   var m = '?c=de.uhilger.filecms.api.FileMgr&m=bildVerkleinern';
404   m = m + '&p=' + pfad; 
405   m = m + '&p=' + fname; 
406   var u = '../svc' + m;
407   fm_get(u, "text", function(resp) {
408     fm_get_list(pfad);
409   });  
410 }
411
bb9f8c 412 function fm_menu_rotate() {
U 413   var gewaehlte = $('.datei-gewaehlt');
414   var fname = $(gewaehlte[0]).text();
415   var m = '?c=de.uhilger.filecms.api.FileMgr&m=bildRotieren';
416   m = m + '&p=' + pfad; 
417   m = m + '&p=' + fname; 
418   var u = '../svc' + m;
419   fm_get(u, "text", function(resp) {
420     fm_get_list(pfad);
421   });  
422 }
423
2f9e6f 424 /* -------- upload ----------- */
U 425
426 function sendFile(datei) {
427   var uri = "../api/upload";
428   var xhr = new XMLHttpRequest();  
429   var fd = new FormData();  
430   xhr.open("POST", uri, true);  
431   xhr.onreadystatechange = function() {  
432     if (xhr.readyState == 4 && xhr.status == 200) {  
433       fm_get_list(pfad);
434       if(dateien.length > 0) {
435         sendFile(dateien.pop());
436       }
437     }  
438   };  
439   fd.append('dateiauswahlfeld', datei);  
440   fd.append('pfad', pfad);
441   xhr.send(fd);          
442 }
443
444 /* -------- helper functions ----------- */
445
446 function fm_get(u, dtype, scallback) {
447   $.ajax({
448     url: u,
449     type: "GET",
450     dataType: dtype,
451     success: scallback,
452     error: function (xhr, status, errorThrown) {
453       alert("Error: " + errorThrown + " Status: " + status + " URL: " + u);
454     },
455     complete: function (xhr, status) {
456       //console.log( "The request is complete!" );
457     }
458
459   });
460 }
461
462 function fm_post(u, d, dtype, scallback) {
463   $.ajax({
464     url: u,
465     data: d,
466     type: "POST",
467     dataType: dtype,
468     success: scallback,
469     error: function (xhr, status, errorThrown) {
470       $('#fehler').html("Error: " + errorThrown + " Status: " + status);
471     },
472     complete: function (xhr, status) {
473       //alert( "The request is complete!" );
474     }
475   });
476 }