Adding basic file browsing support
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Fabian Stamm
2020-10-14 02:52:02 +02:00
parent 78c40e4819
commit 46d8f8b289
48 changed files with 1097 additions and 70 deletions

View File

@ -87,30 +87,38 @@ export function extractPackagePath(path: string): [string, string | undefined] {
return [packageName, packageVersion];
}
import db from "./db.ts";
import type { IPackage } from "./db.ts";
import bucket from "./s3.ts";
export async function getFile(
pkgName: string,
version: string | null | undefined,
file: string
): Promise<{ etag: string; data: Uint8Array } | null | undefined> {
console.log("Searching for file: %s/%s@%s", pkgName, file, version);
const meta = await db.package.findOne({ name: pkgName });
export function getAbsolutePackageVersion(
pkg?: IPackage | null,
version?: string
) {
if (!pkg || pkg.versions.length < 1) return undefined;
if (!meta || meta.versions.length < 1) return null;
const versions = meta.versions.sort(sortVersions).reverse();
const versions = pkg.versions.sort(sortVersions).reverse();
if (!version) {
version = versions[0];
} else {
const v = versions.filter((e) => e.startsWith(version as string));
if (v.length < 1) return null;
if (v.length < 1) return undefined;
version = v[0];
}
return version;
}
export async function getBucketFilePath(
pkgName: string,
version: string,
file: string
) {
if (file.startsWith("/")) {
file = file.substr(1);
}
const bucketPath = (
"packages/" +
pkgName +
@ -120,11 +128,24 @@ export async function getFile(
file
).replace(/@/g, "§");
return bucketPath;
}
export async function getFile(
pkgName: string,
version: string | undefined,
file: string
): Promise<{ etag: string; data: Uint8Array } | null | undefined> {
if (!version) return undefined;
const bucketPath = await getBucketFilePath(pkgName, version, file);
if (!bucketPath) return null;
console.log("Getting file from:", bucketPath);
try {
const res = await bucket.getObject(bucketPath);
if (!res) return undefined;
if (!res || res.body.byteLength === 0) return undefined;
return {
etag: res.etag,
data: res.body,