mirror of
https://github.com/mickael-kerjean/filestash.git
synced 2025-11-01 10:56:31 +08:00
improve (perf): refactor to minimise some loops
This commit is contained in:
@ -12,7 +12,7 @@ class FileSystem{
|
||||
this.current_path = null;
|
||||
}
|
||||
|
||||
ls(path){
|
||||
ls(path, show_hidden = false){
|
||||
this.current_path = path;
|
||||
this.obs && this.obs.complete();
|
||||
return Observable.create((obs) => {
|
||||
@ -20,7 +20,7 @@ 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).then(() => new Promise((done, err) => {
|
||||
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();
|
||||
@ -40,14 +40,11 @@ class FileSystem{
|
||||
});
|
||||
}
|
||||
|
||||
_ls_from_http(path){
|
||||
_ls_from_http(path, show_hidden){
|
||||
const url = appendShareToUrl("/api/files/ls?path="+prepare(path));
|
||||
|
||||
return http_get(url).then((response) => {
|
||||
response.results = (response.results || []).map((f) => {
|
||||
f.path = pathBuilder(path, f.name, f.type);
|
||||
return f;
|
||||
});
|
||||
response = fileMiddleware(response, path, show_hidden);
|
||||
|
||||
return cache.upsert(cache.FILE_PATH, [currentShare(), path], (_files) => {
|
||||
let store = Object.assign({
|
||||
@ -72,13 +69,13 @@ class FileSystem{
|
||||
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[i] = null;
|
||||
_files_virtual_to_keep.splice(i, 1);
|
||||
i -= 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// add stuff that didn't exist in our response
|
||||
_files_virtual_to_keep = _files_virtual_to_keep.filter((e) => e);
|
||||
store.results = store.results.concat(_files_virtual_to_keep);
|
||||
}
|
||||
store.last_update = new Date();
|
||||
@ -361,10 +358,11 @@ class FileSystem{
|
||||
});
|
||||
}
|
||||
|
||||
search(keyword, path = "/"){
|
||||
search(keyword, path = "/", show_hidden){
|
||||
const url = appendShareToUrl("/api/files/search?path="+prepare(path)+"&q="+encodeURIComponent(keyword))
|
||||
return http_get(url).then((res) => {
|
||||
return res.results
|
||||
return http_get(url).then((response) => {
|
||||
response = fileMiddleware(response, path, show_hidden);
|
||||
return response.results;
|
||||
});
|
||||
}
|
||||
|
||||
@ -441,11 +439,11 @@ class FileSystem{
|
||||
last_update: new Date()
|
||||
};
|
||||
}
|
||||
let file = {
|
||||
let file = mutateFile({
|
||||
path: path,
|
||||
name: basename(path),
|
||||
type: /\/$/.test(path) ? 'directory' : 'file'
|
||||
};
|
||||
}, path);
|
||||
if(icon) file.icon = icon;
|
||||
res.results.push(file);
|
||||
return res;
|
||||
@ -468,4 +466,28 @@ class FileSystem{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const createLink = (type, path) => {
|
||||
return type === "file" ? "/view" + path : "/files" + 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){
|
||||
response.results.splice(i, 1);
|
||||
i -= 1;
|
||||
}
|
||||
}
|
||||
return response;
|
||||
};
|
||||
|
||||
const mutateFile = (file, path) => {
|
||||
if(file.path === undefined) {
|
||||
file.path = pathBuilder(path, file.name, file.type);
|
||||
}
|
||||
file.link = createLink(file.type, file.path);
|
||||
return file;
|
||||
};
|
||||
|
||||
export const Files = new FileSystem();
|
||||
|
||||
@ -194,7 +194,6 @@ class MultiStepForm extends React.Component {
|
||||
if(value === "tunnel"){
|
||||
const waitForDomain = (count = 0) => {
|
||||
return this.props.tunnelCall().then((url) => {
|
||||
console.log("- config tunnel_url: %s - %d", url, count);
|
||||
if(url && /\.filestash\.app$/.test(url) === true){
|
||||
return Promise.resolve(url);
|
||||
}
|
||||
|
||||
@ -417,7 +417,3 @@ export const onSearch = (keyword, path = "/") => {
|
||||
.catch((err) => obs.error(err));
|
||||
});
|
||||
};
|
||||
|
||||
export const createLink = (type, path) => {
|
||||
return type === "file" ? "/view" + path : "/files" + path;
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@ import { SelectableGroup } from 'react-selectable';
|
||||
import './filespage.scss';
|
||||
import './error.scss';
|
||||
import { Files } from '../model/';
|
||||
import { sort, onCreate, onRename, onMultiRename, onDelete, onMultiDelete, onUpload, onSearch, createLink } from './filespage.helper';
|
||||
import { sort, onCreate, onRename, onMultiRename, onDelete, onMultiDelete, onUpload, onSearch } from './filespage.helper';
|
||||
import { NgIf, NgShow, Loader, EventReceiver, LoggedInOnly, ErrorPage } from '../components/';
|
||||
import { notify, debounce, goToFiles, goToViewer, event, settings_get, settings_put } from '../helpers/';
|
||||
import { BreadCrumb, FileSystem, FrequentlyAccess, Submenu } from './filespage/';
|
||||
@ -120,24 +120,14 @@ export class FilesPage extends React.Component {
|
||||
|
||||
onRefresh(path = this.state.path){
|
||||
this._cleanupListeners();
|
||||
const observer = Files.ls(path).subscribe((res) => {
|
||||
const observer = Files.ls(path, this.state.show_hidden).subscribe((res) => {
|
||||
if(res.status !== "ok"){
|
||||
notify.send(res, "error");
|
||||
return;
|
||||
}
|
||||
let files = new Array(res.results.length);
|
||||
for(let i=0,l=res.results.length; i<l; i++){
|
||||
let path = this.state.path+res.results[i].name;
|
||||
path = path.replace(/#/g, "%23");
|
||||
if(this.state.show_hidden === false && res.results[i].name[0] === "."){
|
||||
continue;
|
||||
}
|
||||
files[i] = res.results[i];
|
||||
files[i].link = createLink(res.results[i].type, res.results[i].path);
|
||||
}
|
||||
this.setState({
|
||||
metadata: res.metadata,
|
||||
files: sort(files, this.state.sort),
|
||||
files: sort(res.results, this.state.sort),
|
||||
selected: [],
|
||||
loading: false,
|
||||
is_search: false,
|
||||
@ -217,14 +207,7 @@ export class FilesPage extends React.Component {
|
||||
is_search: true,
|
||||
page_number: PAGE_NUMBER_INIT,
|
||||
});
|
||||
this._search = onSearch(search, this.state.path).subscribe((f = []) => {
|
||||
for(let i=0; i<f.length; i++){
|
||||
f[i].link = createLink(f[i].type, f[i].path);
|
||||
if(this.state.show_hidden === false && f[i].path.indexOf("/.") !== -1){
|
||||
f.splice(i, 1);
|
||||
i -= 1;
|
||||
}
|
||||
}
|
||||
this._search = onSearch(search, this.state.path, this.state.show_hidden).subscribe((f = []) => {
|
||||
this.setState({
|
||||
files: sort(f, this.state.sort),
|
||||
loading: false,
|
||||
|
||||
Reference in New Issue
Block a user