maintain (eslint): linting on frontend

This commit is contained in:
Mickael Kerjean
2021-12-21 01:32:37 +11:00
parent 48a6763380
commit 5156432b52
62 changed files with 1920 additions and 1655 deletions

View File

@ -1,18 +1,20 @@
"use strict";
import { http_get, http_post, http_options, prepare, basename, dirname, pathBuilder } from '../helpers/';
import { filetype, currentShare, appendShareToUrl } from '../helpers/';
import {
http_get, http_post, http_options, prepare, basename, dirname, pathBuilder,
currentShare, appendShareToUrl,
} from "../helpers/";
import { Observable } from 'rxjs/Observable';
import { cache } from '../helpers/';
import { Observable } from "rxjs/Observable";
import { cache } from "../helpers/";
class FileSystem{
constructor(){
class FileSystem {
constructor() {
this.obs = null;
this.current_path = null;
}
ls(path, show_hidden = false){
ls(path, show_hidden = false) {
this.current_path = path;
this.obs && this.obs.complete();
return Observable.create((obs) => {
@ -20,19 +22,20 @@ class FileSystem{
let keep_pulling_from_http = false;
this._ls_from_cache(path, true).then((cache) => {
const fetch_from_http = (_path) => {
return this._ls_from_http(_path, show_hidden).then(() => new Promise((done, err) => {
window.setTimeout(() => done(), 2000);
})).then(() => {
if(keep_pulling_from_http === false) return Promise.resolve();
return fetch_from_http(_path);
}).catch((err) => {
if(cache === null){
this.obs && this.obs.error({message: "Unknown Path"});
}
});
return this._ls_from_http(_path, show_hidden)
.then(() => new Promise((done, err) => {
window.setTimeout(() => done(), 2000);
})).then(() => {
if (keep_pulling_from_http === false) return Promise.resolve();
return fetch_from_http(_path);
}).catch((err) => {
if (cache === null) {
this.obs && this.obs.error({ message: "Unknown Path" });
}
});
};
fetch_from_http(path);
}).catch((err) => this.obs.error({message: err && err.message}));
}).catch((err) => this.obs.error({ message: err && err.message }));
return () => {
keep_pulling_from_http = false;
@ -40,33 +43,33 @@ class FileSystem{
});
}
_ls_from_http(path, show_hidden){
const url = appendShareToUrl("/api/files/ls?path="+prepare(path));
_ls_from_http(path, show_hidden) {
const url = appendShareToUrl("/api/files/ls?path=" + prepare(path));
return http_get(url).then((response) => {
response = fileMiddleware(response, path, show_hidden);
return cache.upsert(cache.FILE_PATH, [currentShare(), path], (_files) => {
let store = Object.assign({
const store = Object.assign({
share: currentShare(),
status: "ok",
path: path,
results: null,
access_count: 0,
metadata: null
metadata: null,
}, _files);
store.metadata = response.metadata;
store.results = response.results;
if(_files && _files.results){
if (_files && _files.results) {
store.access_count = _files.access_count;
// find out which entry we want to keep from the cache
let _files_virtual_to_keep = _files.results.filter((file) => {
return file.icon === 'loading';
const _files_virtual_to_keep = _files.results.filter((file) => {
return file.icon === "loading";
});
// update file results when something is going on
for(let i=0; i<_files_virtual_to_keep.length; i++){
for(let j=0; j<store.results.length; j++){
if(store.results[j].name === _files_virtual_to_keep[i].name){
for (let i=0; i<_files_virtual_to_keep.length; i++) {
for (let j=0; j<store.results.length; j++) {
if (store.results[j].name === _files_virtual_to_keep[i].name) {
store.results[j] = Object.assign({}, _files_virtual_to_keep[i]);
_files_virtual_to_keep.splice(i, 1);
i -= 1;
@ -81,41 +84,41 @@ class FileSystem{
store.last_access = new Date();
return store;
}).catch(() => Promise.resolve(response)).then((data) => {
if(this.current_path === path){
if (this.current_path === path) {
this.obs && this.obs.next(data);
}
return Promise.resolve(null);
});
}).catch((_err) => {
if(_err.code === "Unauthorized"){
location = "/login?next="+location.pathname;
if (_err.code === "Unauthorized") {
location = "/login?next=" + location.pathname;
}
this.obs.next(_err);
return Promise.reject(_err);
})
});
}
_ls_from_cache(path, _record_access = false){
_ls_from_cache(path, _record_access = false) {
return cache.get(cache.FILE_PATH, [currentShare(), path]).then((response) => {
if(!response || !response.results) return null;
if(this.current_path === path){
if (!response || !response.results) return null;
if (this.current_path === path) {
this.obs && this.obs.next({
status: 'ok',
status: "ok",
results: response.results,
metadata: response.metadata
metadata: response.metadata,
});
}
return response;
}).then((e) => {
requestAnimationFrame(() => {
if(_record_access === true){
if (_record_access === true) {
cache.upsert(cache.FILE_PATH, [currentShare(), path], (response) => {
if(!response || !response.results) return null;
if(this.current_path === path){
if (!response || !response.results) return null;
if (this.current_path === path) {
this.obs && this.obs.next({
status: 'ok',
status: "ok",
results: response.results,
metadata: response.metadata
metadata: response.metadata,
});
}
response.last_access = new Date();
@ -128,40 +131,43 @@ class FileSystem{
});
}
rm(path){
const url = appendShareToUrl('/api/files/rm?path='+prepare(path));
return this._replace(path, 'loading')
.then((res) => this.current_path === dirname(path) ? this._ls_from_cache(dirname(path)) : Promise.resolve(res))
rm(path) {
const url = appendShareToUrl("/api/files/rm?path=" + prepare(path));
return this._replace(path, "loading")
.then((res) => this.current_path === dirname(path) ?
this._ls_from_cache(dirname(path)) : Promise.resolve(res))
.then(() => http_get(url))
.then((res) => {
return cache.remove(cache.FILE_CONTENT, [currentShare(), path])
.then(cache.remove(cache.FILE_CONTENT, [currentShare(), path], false))
.then(cache.remove(cache.FILE_PATH, [currentShare(), dirname(path)], false))
.then(this._remove(path, 'loading'))
.then((res) => this.current_path === dirname(path) ? this._ls_from_cache(dirname(path)) : Promise.resolve(res))
.then(this._remove(path, "loading"))
.then((res) => this.current_path === dirname(path) ?
this._ls_from_cache(dirname(path)) : Promise.resolve(res));
})
.catch((err) => {
return this._replace(path, 'error', 'loading')
.then((res) => this.current_path === dirname(path) ? this._ls_from_cache(dirname(path)) : Promise.resolve(res))
return this._replace(path, "error", "loading")
.then((res) => this.current_path === dirname(path) ?
this._ls_from_cache(dirname(path)) : Promise.resolve(res))
.then(() => Promise.reject(err));
});
}
cat(path){
const url = appendShareToUrl('/api/files/cat?path='+prepare(path));
return http_get(url, 'raw')
cat(path) {
const url = appendShareToUrl("/api/files/cat?path=" + prepare(path));
return http_get(url, "raw")
.then((res) => {
if(this.is_binary(res) === true){
return Promise.reject({code: 'BINARY_FILE'});
if (this.is_binary(res) === true) {
return Promise.reject({ code: "BINARY_FILE" });
}
return cache.upsert(cache.FILE_CONTENT, [currentShare(), path], (response) => {
let file = response? response : {
const file = response ? response : {
share: currentShare(),
path: path,
last_update: null,
last_access: null,
access_count: -1,
result: null
result: null,
};
file.result = res;
file.access_count += 1;
@ -171,63 +177,66 @@ class FileSystem{
});
}
zip(paths){
const url = appendShareToUrl('/api/files/zip?'+paths.map((p)=> "path=" +prepare(p)).join("&"))
zip(paths) {
const url = appendShareToUrl(
"/api/files/zip?" + paths.map((p) => "path=" + prepare(p)).join("&"),
);
window.open(url);
return Promise.resolve();
}
options(path){
const url = appendShareToUrl('/api/files/cat?path='+prepare(path));
options(path) {
const url = appendShareToUrl("/api/files/cat?path=" + prepare(path));
return http_options(url);
}
url(path){
const url = appendShareToUrl('/api/files/cat?path='+prepare(path));
url(path) {
const url = appendShareToUrl("/api/files/cat?path=" + prepare(path));
return Promise.resolve(url);
}
save(path, file){
const url = appendShareToUrl('/api/files/cat?path='+prepare(path));
return this._replace(path, 'loading')
.then(() => http_post(url, file, 'blob'))
save(path, file) {
const url = appendShareToUrl("/api/files/cat?path=" + prepare(path));
return this._replace(path, "loading")
.then(() => http_post(url, file, "blob"))
.then(() => {
return this._saveFileToCache(path, file)
.then(() => this._replace(path, null, 'loading'))
.then(() => this._replace(path, null, "loading"))
.then(() => this._refresh(path));
})
.catch((err) => {
return this._replace(path, 'error', 'loading')
return this._replace(path, "error", "loading")
.then(() => this._refresh(path))
.then(() => Promise.reject(err));
});
}
mkdir(path, step){
const url = appendShareToUrl('/api/files/mkdir?path='+prepare(path)),
origin_path = pathBuilder(this.current_path, basename(path), 'directoy'),
destination_path = path;
mkdir(path, step) {
const url = appendShareToUrl("/api/files/mkdir?path=" + prepare(path));
const origin_path = pathBuilder(this.current_path, basename(path), "directoy");
const destination_path = path;
const action_prepare = (part_of_a_batch_operation = false) => {
if(part_of_a_batch_operation === true){
return this._add(destination_path, 'loading')
if (part_of_a_batch_operation === true) {
return this._add(destination_path, "loading")
.then(() => this._refresh(destination_path));
}
return this._add(destination_path, 'loading')
.then(() => origin_path !== destination_path ? this._add(origin_path, 'loading') : Promise.resolve())
return this._add(destination_path, "loading")
.then(() => origin_path !== destination_path ?
this._add(origin_path, "loading") : Promise.resolve())
.then(() => this._refresh(origin_path, destination_path));
};
const action_execute = (part_of_a_batch_operation = false) => {
if(part_of_a_batch_operation === true){
if (part_of_a_batch_operation === true) {
return http_get(url)
.then(() => {
return this._replace(destination_path, null, 'loading')
return this._replace(destination_path, null, "loading")
.then(() => this._refresh(destination_path));
})
.catch((err) => {
this._replace(destination_path, 'error', 'loading')
this._replace(destination_path, "error", "loading")
.then(() => this._refresh(origin_path, destination_path));
return Promise.reject(err);
});
@ -235,60 +244,63 @@ class FileSystem{
return http_get(url)
.then(() => {
return this._replace(destination_path, null, 'loading')
.then(() => origin_path !== destination_path ? this._remove(origin_path, 'loading') : Promise.resolve())
return this._replace(destination_path, null, "loading")
.then(() => origin_path !== destination_path ?
this._remove(origin_path, "loading") : Promise.resolve())
.then(() => cache.add(cache.FILE_PATH, [currentShare(), destination_path], {
path: destination_path,
share: currentShare(),
results: [],
access_count: 0,
last_access: null,
last_update: new Date()
last_update: new Date(),
}))
.then(() => this._refresh(origin_path, destination_path));
})
.catch((err) => {
this._replace(origin_path, 'error', 'loading')
.then(() => origin_path !== destination_path ? this._remove(destination_path, 'loading') : Promise.resolve())
this._replace(origin_path, "error", "loading")
.then(() => origin_path !== destination_path ?
this._remove(destination_path, "loading") : Promise.resolve())
.then(() => this._refresh(origin_path, destination_path));
return Promise.reject(err);
});
};
if(step === 'prepare_only'){
if (step === "prepare_only") {
return action_prepare(true);
}else if(step === 'execute_only'){
} else if (step === "execute_only") {
return action_execute(true);
}else{
} else {
return action_prepare().then(action_execute);
}
}
touch(path, file, step, params){
const origin_path = pathBuilder(this.current_path, basename(path), 'file'),
destination_path = path;
touch(path, file, step, params) {
const origin_path = pathBuilder(this.current_path, basename(path), "file");
const destination_path = path;
const action_prepare = (part_of_a_batch_operation = false) => {
if(part_of_a_batch_operation === true){
return this._add(destination_path, 'loading')
if (part_of_a_batch_operation === true) {
return this._add(destination_path, "loading")
.then(() => this._refresh(destination_path));
}else{
return this._add(destination_path, 'loading')
.then(() => origin_path !== destination_path ? this._add(origin_path, 'loading') : Promise.resolve())
} else {
return this._add(destination_path, "loading")
.then(() => origin_path !== destination_path ?
this._add(origin_path, "loading") : Promise.resolve())
.then(() => this._refresh(origin_path, destination_path));
}
};
const action_execute = (part_of_a_batch_operation = false) => {
if(part_of_a_batch_operation === true){
if (part_of_a_batch_operation === true) {
return query()
.then(() => {
return this._replace(destination_path, null, 'loading')
return this._replace(destination_path, null, "loading")
.then(() => this._refresh(destination_path));
})
.catch((err) => {
this._replace(destination_path, null, 'error')
.then(() => this._replace(destination_path, null, 'loading'))
this._replace(destination_path, null, "error")
.then(() => this._replace(destination_path, null, "loading"))
.then(() => this._refresh(destination_path));
return Promise.reject(err);
});
@ -296,49 +308,51 @@ class FileSystem{
return query()
.then(() => {
return this._saveFileToCache(path, file)
.then(() => this._replace(destination_path, null, 'loading'))
.then(() => origin_path !== destination_path ? this._remove(origin_path, 'loading') : Promise.resolve())
.then(() => this._replace(destination_path, null, "loading"))
.then(() => origin_path !== destination_path ?
this._remove(origin_path, "loading") : Promise.resolve())
.then(() => this._refresh(origin_path, destination_path));
})
.catch((err) => {
this._replace(origin_path, 'error', 'loading')
.then(() => origin_path !== destination_path ? this._remove(destination_path, 'loading') : Promise.resolve())
this._replace(origin_path, "error", "loading")
.then(() => origin_path !== destination_path ?
this._remove(destination_path, "loading") : Promise.resolve())
.then(() => this._refresh(origin_path, destination_path));
return Promise.reject(err);
});
function query(){
if(file){
const url = appendShareToUrl('/api/files/cat?path='+prepare(path));
return http_post(url, file, 'blob', params);
}else{
const url = appendShareToUrl('/api/files/touch?path='+prepare(path));
function query() {
if (file) {
const url = appendShareToUrl("/api/files/cat?path=" + prepare(path));
return http_post(url, file, "blob", params);
} else {
const url = appendShareToUrl("/api/files/touch?path=" + prepare(path));
return http_get(url);
}
}
};
if(step === 'prepare_only'){
if (step === "prepare_only") {
return action_prepare(true);
}else if(step === 'execute_only'){
} else if (step === "execute_only") {
return action_execute(true);
}else{
} else {
return action_prepare().then(action_execute);
}
}
mv(from, to){
const url = appendShareToUrl('/api/files/mv?from='+prepare(from)+"&to="+prepare(to)),
origin_path = from,
destination_path = to;
mv(from, to) {
const url = appendShareToUrl("/api/files/mv?from=" + prepare(from) + "&to=" + prepare(to));
const origin_path = from;
const destination_path = to;
return this._replace(origin_path, 'loading')
.then(this._add(destination_path, 'loading'))
return this._replace(origin_path, "loading")
.then(this._add(destination_path, "loading"))
.then(() => this._refresh(origin_path, destination_path))
.then(() => http_get(url))
.then((res) => {
return this._remove(origin_path, 'loading')
.then(() => this._replace(destination_path, null, 'loading'))
return this._remove(origin_path, "loading")
.then(() => this._replace(destination_path, null, "loading"))
.then(() => this._refresh(origin_path, destination_path))
.then(() => {
cache.update(cache.FILE_PATH, [currentShare(), origin_path], (data) => {
@ -353,56 +367,62 @@ class FileSystem{
});
})
.catch((err) => {
this._replace(origin_path, 'error', 'loading')
.then(() => this._remove(destination_path, 'loading'))
.then(() => this._refresh(origin_path, destination_path))
this._replace(origin_path, "error", "loading")
.then(() => this._remove(destination_path, "loading"))
.then(() => this._refresh(origin_path, destination_path));
return Promise.reject(err);
});
}
search(keyword, path = "/", show_hidden){
const url = appendShareToUrl("/api/files/search?path="+prepare(path)+"&q="+encodeURIComponent(keyword))
search(keyword, path = "/", show_hidden) {
const url = appendShareToUrl(
"/api/files/search?path=" + prepare(path) +
"&q="+encodeURIComponent(keyword),
);
return http_get(url).then((response) => {
response = fileMiddleware(response, path, show_hidden);
return response.results;
});
}
frequents(){
let data = [];
frequents() {
const data = [];
return cache.fetchAll((value) => {
if(value.access_count >= 1 && value.path !== "/"){
if (value.access_count >= 1 && value.path !== "/") {
data.push(value);
}
}, cache.FILE_PATH, [currentShare(), "/"]).then(() => {
return Promise.resolve(
data
.sort((a,b) => a.access_count > b.access_count? -1 : 1)
.sort((a, b) => a.access_count > b.access_count? -1 : 1)
.map((a) => a.path)
.slice(0,6)
.slice(0, 6),
);
});
}
_saveFileToCache(path, file){
if(!file) return update_cache("");
_saveFileToCache(path, file) {
if (!file) return update_cache("");
return new Promise((done, err) => {
const reader = new FileReader();
reader.readAsText(file);
reader.onload = () => this.is_binary(reader.result) === false? update_cache(reader.result).then(done) : done();
reader.onload = () => this.is_binary(reader.result) === false ?
update_cache(reader.result).then(done) : done();
reader.onerror = (_err) => err(_err);
});
function update_cache(result){
function update_cache(result) {
return cache.upsert(cache.FILE_CONTENT, [currentShare(), path], (response) => {
if(!response) response = {
share: currentShare(),
path: path,
last_access: null,
last_update: null,
result: null,
access_count: 0
};
if (!response) {
response = {
share: currentShare(),
path: path,
last_access: null,
last_update: null,
result: null,
access_count: 0,
};
}
response.last_update = new Date();
response.result = result;
return response;
@ -410,51 +430,55 @@ class FileSystem{
}
}
_refresh(origin_path, destination_path){
if(this.current_path === dirname(origin_path) ||
this.current_path === dirname(destination_path)){
_refresh(origin_path, destination_path) {
if (this.current_path === dirname(origin_path) ||
this.current_path === dirname(destination_path)) {
return this._ls_from_cache(this.current_path);
}
return Promise.resolve();
}
_replace(path, icon, icon_previous){
return cache.update(cache.FILE_PATH, [currentShare(), dirname(path)], function(res){
_replace(path, icon, icon_previous) {
return cache.update(cache.FILE_PATH, [currentShare(), dirname(path)], function(res) {
res.results = res.results.map((file) => {
if(file.name === basename(path) && file.icon == icon_previous){
if(!icon){ delete file.icon; }
if(icon){ file.icon = icon; }
if (file.name === basename(path) && file.icon == icon_previous) {
if (!icon) {
delete file.icon;
}
if (icon) {
file.icon = icon;
}
}
return file;
});
return res;
});
}
_add(path, icon){
_add(path, icon) {
return cache.upsert(cache.FILE_PATH, [currentShare(), dirname(path)], (res) => {
if(!res || !res.results){
if (!res || !res.results) {
res = {
path: path,
share: currentShare(),
results: [],
access_count: 0,
last_access: null,
last_update: new Date()
last_update: new Date(),
};
}
let file = mutateFile({
const file = mutateFile({
path: path,
name: basename(path),
type: /\/$/.test(path) ? 'directory' : 'file'
type: /\/$/.test(path) ? "directory" : "file",
}, path);
if(icon) file.icon = icon;
if (icon) file.icon = icon;
res.results.push(file);
return res;
});
}
_remove(path, previous_icon){
return cache.update(cache.FILE_PATH, [currentShare(), dirname(path)], function(res){
if(!res) return null;
_remove(path, previous_icon) {
return cache.update(cache.FILE_PATH, [currentShare(), dirname(path)], function(res) {
if (!res) return null;
res.results = res.results.filter((file) => {
return file.name === basename(path) && file.icon == previous_icon ? false : true;
});
@ -463,7 +487,7 @@ class FileSystem{
}
is_binary(str){
is_binary(str) {
// Reference: https://en.wikipedia.org/wiki/Specials_(Unicode_block)#Replacement_character
return /\ufffd/.test(str);
}
@ -475,9 +499,9 @@ const createLink = (type, path) => {
};
const fileMiddleware = (response, path, show_hidden) => {
for(let i=0; i<response.results.length; i++){
let f = mutateFile(response.results[i], path);
if(show_hidden === false && f.path.indexOf("/.") !== -1){
for (let i=0; i<response.results.length; i++) {
const f = mutateFile(response.results[i], path);
if (show_hidden === false && f.path.indexOf("/.") !== -1) {
response.results.splice(i, 1);
i -= 1;
}
@ -486,7 +510,7 @@ const fileMiddleware = (response, path, show_hidden) => {
};
const mutateFile = (file, path) => {
if(file.path === undefined) {
if (file.path === undefined) {
file.path = pathBuilder(path, file.name, file.type);
}
file.link = createLink(file.type, file.path);