Skip to content

Commit

Permalink
feat: sizes in bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
TimMikeladze committed Jun 21, 2024
1 parent 2ca55d0 commit b0808b6
Show file tree
Hide file tree
Showing 4 changed files with 2,576 additions and 2,529 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,5 @@
"react-dom": ">=17"
},
"resolutions": {},
"dependencies": {
"human-format": "^1.2.0"
}
"dependencies": {}
}
55 changes: 52 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface DirectoryListingItem {
modifiedAt: number;
name: string;
path: string;
size: string | null;
size: number | null;
type: 'file' | 'directory';
}

Expand Down Expand Up @@ -43,14 +43,15 @@ export const parseDirectoryListingHtml = (
const modifiedAt = new Date(modifiedAtText).getTime();
const sizeText = cells[3].textContent?.trim() || '';
const size = sizeText === '-' ? null : sizeText;
const parsedSize = size ? parseFileSize(size) : null;
const description = cells[4].textContent?.trim() || '';

files.push({
type,
name,
path,
modifiedAt,
size,
size: parsedSize,
description,
});
});
Expand All @@ -59,7 +60,9 @@ export const parseDirectoryListingHtml = (
};

export interface FetchDirectoryListingArgs {
concurrency?: number;
fetchFn?: (url: string) => Promise<Response>;

url: string;
}

Expand Down Expand Up @@ -88,14 +91,60 @@ export const fetchDirectoryListing = async (
},
newPath
);
items.push(...subDirectoryItems);
items.push(
...subDirectoryItems.map((x) => ({
...x,
path: [baseUrl, newPath, x.path]
.join('/')
.replace(/([^:]\/)\/+/g, '$1'),
}))
);
})
);

return items;
};

export const parseFileSize = (size: string): number => {
// Regular expression to match the size with optional units
const regex = /^(\d+(\.\d+)?)\s*(B|K|KB|M|MB|G|GB|T|TB|P|PB)?$/i;
const match = size.match(regex);

if (!match) {
throw new Error(`Invalid file size format: ${size}`);
}

const value = parseFloat(match[1]);
const unit = match[3] ? match[3].toUpperCase() : 'B'; // Default to 'B' if no unit is specified

// Define unit multipliers
const units: { [key: string]: number } = {
B: 1,
K: 1024,
KB: 1024,
M: 1024 * 1024,
MB: 1024 * 1024,
G: 1024 * 1024 * 1024,
GB: 1024 * 1024 * 1024,
T: 1024 * 1024 * 1024 * 1024,
TB: 1024 * 1024 * 1024 * 1024,
P: 1024 * 1024 * 1024 * 1024 * 1024,
PB: 1024 * 1024 * 1024 * 1024 * 1024,
};

if (!units[unit]) {
throw new Error(`Unknown unit: ${unit}`);
}

// Convert to bytes and round to the nearest integer
const bytes = value * units[unit];
return Math.round(bytes);
};

export interface ScrapeDirectoryListingArgs {
concurrency?: number;
fetchFn?: () => Promise<Response>;

snapshotFn?: () => Promise<void>;

url: string;
Expand Down
Loading

0 comments on commit b0808b6

Please sign in to comment.