Skip to content

Commit

Permalink
Revert 00c2e96
Browse files Browse the repository at this point in the history
  • Loading branch information
John Schulz committed Dec 15, 2020
1 parent 581568c commit 3ae9d72
Showing 1 changed file with 19 additions and 38 deletions.
57 changes: 19 additions & 38 deletions x-pack/plugins/fleet/server/routes/epm/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ import {
removeInstallation,
getLimitedPackages,
getInstallationObject,
getInstallation,
} from '../../services/epm/packages';
import { defaultIngestErrorHandler, ingestErrorToResponseOptions } from '../../errors';
import { splitPkgKey } from '../../services/epm/registry';
import { licenseService } from '../../services';
import { getArchiveEntry } from '../../services/epm/archive/cache';
import { bufferToStream } from '../../services/epm/streams';

export const getCategoriesHandler: RequestHandler<
undefined,
Expand Down Expand Up @@ -107,51 +107,32 @@ export const getFileHandler: RequestHandler<TypeOf<typeof GetFileRequestSchema.p
try {
const { pkgName, pkgVersion, filePath } = request.params;
const savedObjectsClient = context.core.savedObjects.client;
const installation = await getInstallation({ savedObjectsClient, pkgName });
const useLocalFile = pkgVersion === installation?.version;

if (useLocalFile) {
const assetPath = `${pkgName}-${pkgVersion}/${filePath}`;
const fileBuffer = getArchiveEntry(assetPath);
// only pull local installation if we don't have it cached
const storedAsset = !fileBuffer && (await getAsset({ savedObjectsClient, path: assetPath }));

// error, if neither is available
if (!fileBuffer && !storedAsset) {
const savedObject = await getInstallationObject({ savedObjectsClient, pkgName });
const pkgInstallSource = savedObject?.attributes.install_source;
// TODO: when package storage is available, remove installSource check and check cache and storage, remove registry call
if (pkgInstallSource === 'upload' && pkgVersion === savedObject?.attributes.version) {
const headerContentType = mime.contentType(path.extname(filePath));
if (!headerContentType) {
return response.custom({
body: `installed package file not found: ${filePath}`,
statusCode: 404,
body: `unknown content type for file: ${filePath}`,
statusCode: 400,
});
}

// if storedAsset is not available, fileBuffer *must* be
// b/c we error if we don't have at least one, and storedAsset is the least likely
const { buffer, contentType } = storedAsset
? {
contentType: storedAsset.media_type,
buffer: storedAsset.data_utf8
? Buffer.from(storedAsset.data_utf8, 'utf8')
: Buffer.from(storedAsset.data_base64, 'base64'),
}
: {
contentType: mime.contentType(path.extname(assetPath)),
buffer: fileBuffer,
};

if (!contentType) {
const archiveFile = getArchiveEntry(`${pkgName}-${pkgVersion}/${filePath}`);
if (!archiveFile) {
return response.custom({
body: `unknown content type for file: ${filePath}`,
statusCode: 400,
body: `uploaded package file not found: ${filePath}`,
statusCode: 404,
});
}

const headers: ResponseHeaders = {
'cache-control': 'max-age=10, public',
'content-type': headerContentType,
};
return response.custom({
body: buffer,
body: bufferToStream(archiveFile),
statusCode: 200,
headers: {
'cache-control': 'max-age=10, public',
'content-type': contentType,
},
headers,
});
} else {
const registryResponse = await getFile(pkgName, pkgVersion, filePath);
Expand Down

0 comments on commit 3ae9d72

Please sign in to comment.