Files
superset2/superset-frontend/src/utils/export.ts
dependabot[bot] f2d80a183e chore(deps): bump content-disposition from 1.1.0 to 2.0.0 in /superset-frontend (#40109)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Claude <claude@anthropic.com>
2026-05-20 10:41:34 -07:00

98 lines
3.2 KiB
TypeScript

/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { SupersetClient } from '@superset-ui/core';
import { logging } from '@apache-superset/core/utils';
import rison from 'rison';
import { parse as parseContentDisposition } from 'content-disposition';
// Maximum blob size for in-memory downloads (100MB)
const MAX_BLOB_SIZE = 100 * 1024 * 1024;
/**
* Downloads a blob as a file using a temporary anchor element
* @param blob - The blob to download
* @param fileName - The filename to use for the download
*/
function downloadBlob(blob: Blob, fileName: string): void {
const url = window.URL.createObjectURL(blob);
try {
const a = document.createElement('a');
a.href = url;
a.download = fileName;
a.style.display = 'none';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} finally {
window.URL.revokeObjectURL(url);
}
}
export default async function handleResourceExport(
resource: string,
ids: number[],
done: () => void,
): Promise<void> {
const endpoint = `/api/v1/${resource}/export/?q=${rison.encode(ids)}`;
try {
// Use fetch with blob response instead of iframe to avoid CSP frame-src violations
const response = await SupersetClient.get({
endpoint,
headers: {
Accept: 'application/zip, application/x-zip-compressed, text/plain',
},
parseMethod: 'raw',
});
// Check content length to prevent memory issues with large exports
const contentLength = response.headers.get('Content-Length');
if (contentLength && parseInt(contentLength, 10) > MAX_BLOB_SIZE) {
logging.warn(
`Export file size (${contentLength} bytes) exceeds maximum blob size (${MAX_BLOB_SIZE} bytes). Large exports may cause memory issues.`,
);
}
// Parse filename from Content-Disposition header
const disposition = response.headers.get('Content-Disposition');
let fileName = `${resource}_export.zip`;
if (disposition) {
try {
const parsed = parseContentDisposition(disposition);
if (parsed?.parameters?.filename) {
fileName = parsed.parameters.filename;
}
} catch (error) {
logging.warn('Failed to parse Content-Disposition header:', error);
}
}
// Convert response to blob and trigger download
const blob = await response.blob();
downloadBlob(blob, fileName);
done();
} catch (error) {
logging.error('Resource export failed:', error);
done();
throw error;
}
}