Skip to content

Commit

Permalink
Clean up download endpoint and move logic to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Leon committed Nov 2, 2023
1 parent 7335bae commit c7e4904
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
26 changes: 2 additions & 24 deletions apps/api/src/handlers/downloads.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import torrent from "../models/torrent";
import fs from "fs";
import { sign, verify, decode } from "hono/jwt";
import { sign, verify } from "hono/jwt";
import { findLargestFileInPath } from "@packages/utils/std/files";

export const downloads = async (ctx) => {
const data = await torrent.downloads();
Expand Down Expand Up @@ -80,25 +80,3 @@ export const download = async (ctx) => {

return response;
};

const findLargestFileInPath = (path): string => {
console.log("finding largest file in path:", path);

if (!fs.lstatSync(path).isDirectory()) {
return path;
}

const files = fs.readdirSync(path);

const largest = files
.map((file) => {
const stat = fs.statSync(`${path}/${file}`);
return {
file,
size: stat.size,
};
})
.sort((a, b) => b.size - a.size)[0];

return `${path}/${largest.file}`;
};
1 change: 0 additions & 1 deletion apps/navigator/src/pages/Home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { route } from 'preact-router';

import { useEffect, useState, useContext } from "preact/hooks";
import api from '@/models/api'
import { bytes } from '@packages/utils/std/normalise';
import { SearchBtn, SecondaryBtn } from '@/components/Button'
import Loader from '@/state/loader.js';
import Search from '@/state/search.js';
Expand Down
23 changes: 23 additions & 0 deletions packages/utils/std/files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import fs from "fs";

export const findLargestFileInPath = (path): string => {
console.log("finding largest file in path:", path);

if (!fs.lstatSync(path).isDirectory()) {
return path;
}

const files = fs.readdirSync(path);

const largest = files
.map((file) => {
const stat = fs.statSync(`${path}/${file}`);
return {
file,
size: stat.size,
};
})
.sort((a, b) => b.size - a.size)[0];

return `${path}/${largest.file}`;
};

0 comments on commit c7e4904

Please sign in to comment.