Do cache || saved_object || network in http handler

This commit is contained in:
John Schulz 2020-12-08 14:50:33 -05:00
parent cf0d56797f
commit e33d7d417b

View file

@ -17,6 +17,7 @@ import {
BulkInstallPackageInfo, BulkInstallPackageInfo,
BulkInstallPackagesResponse, BulkInstallPackagesResponse,
IBulkInstallPackageHTTPError, IBulkInstallPackageHTTPError,
ASSETS_SAVED_OBJECT_TYPE,
} from '../../../common'; } from '../../../common';
import { import {
GetCategoriesRequestSchema, GetCategoriesRequestSchema,
@ -41,12 +42,13 @@ import {
removeInstallation, removeInstallation,
getLimitedPackages, getLimitedPackages,
getInstallationObject, getInstallationObject,
getInstallation,
} from '../../services/epm/packages'; } from '../../services/epm/packages';
import { defaultIngestErrorHandler, ingestErrorToResponseOptions } from '../../errors'; import { defaultIngestErrorHandler, ingestErrorToResponseOptions } from '../../errors';
import { splitPkgKey } from '../../services/epm/registry'; import { splitPkgKey } from '../../services/epm/registry';
import { licenseService } from '../../services'; import { licenseService } from '../../services';
import { getArchiveEntry } from '../../services/epm/archive/cache'; import { getArchiveEntry } from '../../services/epm/archive/cache';
import { bufferToStream } from '../../services/epm/streams'; import { PackageAsset, assetPathToObjectId } from '../../services/epm/archive/save_to_es';
export const getCategoriesHandler: RequestHandler< export const getCategoriesHandler: RequestHandler<
undefined, undefined,
@ -107,32 +109,46 @@ export const getFileHandler: RequestHandler<TypeOf<typeof GetFileRequestSchema.p
try { try {
const { pkgName, pkgVersion, filePath } = request.params; const { pkgName, pkgVersion, filePath } = request.params;
const savedObjectsClient = context.core.savedObjects.client; const savedObjectsClient = context.core.savedObjects.client;
const savedObject = await getInstallationObject({ savedObjectsClient, pkgName }); const installation = await getInstallation({ savedObjectsClient, pkgName });
const pkgInstallSource = savedObject?.attributes.install_source; const useLocalFile = pkgVersion === installation?.version;
// TODO: when package storage is available, remove installSource check and check cache and storage, remove registry call
if (pkgInstallSource === 'upload' && pkgVersion === savedObject?.attributes.version) { if (useLocalFile) {
const headerContentType = mime.contentType(path.extname(filePath)); const archiveKey = `${pkgName}-${pkgVersion}/${filePath}`;
const archiveEntry = getArchiveEntry(archiveKey);
const assetSavedObject = await savedObjectsClient.get<PackageAsset>(
ASSETS_SAVED_OBJECT_TYPE,
assetPathToObjectId(archiveKey)
);
if (!archiveEntry && !assetSavedObject) {
return response.custom({
body: `installed package file not found: ${filePath}`,
statusCode: 404,
});
}
const headerContentType =
assetSavedObject.attributes.media_type || mime.contentType(path.extname(archiveKey));
if (!headerContentType) { if (!headerContentType) {
return response.custom({ return response.custom({
body: `unknown content type for file: ${filePath}`, body: `unknown content type for file: ${filePath}`,
statusCode: 400, statusCode: 400,
}); });
} }
const archiveFile = getArchiveEntry(`${pkgName}-${pkgVersion}/${filePath}`);
if (!archiveFile) { const { data_base64: base64, data_utf8: utf8 } = assetSavedObject.attributes;
return response.custom({ // if we have a local Buffer, use that
body: `uploaded package file not found: ${filePath}`, // else, create one from the saved object (try utf8 first)
statusCode: 404, const responseBody =
}); archiveEntry || utf8 ? Buffer.from(utf8, 'utf8') : Buffer.from(base64, 'base64');
}
const headers: ResponseHeaders = {
'cache-control': 'max-age=10, public',
'content-type': headerContentType,
};
return response.custom({ return response.custom({
body: bufferToStream(archiveFile), body: responseBody,
statusCode: 200, statusCode: 200,
headers, headers: {
'cache-control': 'max-age=10, public',
'content-type': headerContentType,
},
}); });
} else { } else {
const registryResponse = await getFile(`/package/${pkgName}/${pkgVersion}/${filePath}`); const registryResponse = await getFile(`/package/${pkgName}/${pkgVersion}/${filePath}`);