Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added error handling for ENAMETOOLONG import error #16054

Merged
merged 3 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion ghost/admin/app/components/modal-import-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ModalComponent from 'ghost-admin/components/modal-base';
import ghostPaths from 'ghost-admin/utils/ghost-paths';
import {GENERIC_ERROR_MESSAGE} from 'ghost-admin/services/notifications';
import {computed} from '@ember/object';
import {getErrorCode} from '../services/ajax';
import {inject} from 'ghost-admin/decorators/inject';
import {
isRequestEntityTooLargeError,
Expand Down Expand Up @@ -90,7 +91,9 @@ export default ModalComponent.extend({
this.notifications.showAPIError(error);
}

if (isUnsupportedMediaTypeError(error)) {
if (getErrorCode(error) === 'INVALID_ZIP_FILE_NAME_ENCODING') {
message = 'The uploaded zip could not be read due to a long or invalid file name. Please remove any special characters from the file name, or alternatively try another archiving tool if using MacOS Archive Utility.';
} else if (isUnsupportedMediaTypeError(error)) {
message = 'The file type you uploaded is not supported.';
} else if (isRequestEntityTooLargeError(error)) {
message = 'The file you uploaded was larger than the maximum file size your server allows.';
Expand Down
11 changes: 11 additions & 0 deletions ghost/admin/app/services/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ export function isUnsupportedMediaTypeError(errorOrStatus) {
}
}

/**
* Returns the code (from the payload) from an error object.
* @returns {string|null} error code
*/
export function getErrorCode(errorOrStatus) {
if (isAjaxError(errorOrStatus) && errorOrStatus.payload && errorOrStatus.payload.errors && Array.isArray(errorOrStatus.payload.errors) && errorOrStatus.payload.errors.length > 0) {
return errorOrStatus.payload.errors[0].code || null;
}
return null;
}

/* Maintenance error */

export class MaintenanceError extends AjaxError {
Expand Down
26 changes: 21 additions & 5 deletions ghost/core/core/server/data/importer/import-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ const messages = {
noContentToImport: 'Zip did not include any content to import.',
invalidZipStructure: 'Invalid zip file structure.',
invalidZipFileBaseDirectory: 'Invalid zip file: base directory read failed',
zipContainsMultipleDataFormats: 'Zip file contains multiple data formats. Please split up and import separately.'
zipContainsMultipleDataFormats: 'Zip file contains multiple data formats. Please split up and import separately.',
invalidZipFileNameEncoding: 'The uploaded zip could not be read',
invalidZipFileNameEncodingContext: 'The filename was too long or contained invalid characters',
invalidZipFileNameEncodingHelp: 'Remove any special characters from the file name, or alternatively try another archiving tool if using MacOS Archive Utility'
};

// Glob levels
Expand Down Expand Up @@ -170,13 +173,26 @@ class ImportManager {
* @param {string} filePath
* @returns {Promise<string>} full path to the extracted folder
*/
extractZip(filePath) {
async extractZip(filePath) {
const tmpDir = path.join(os.tmpdir(), uuid.v4());
this.fileToDelete = tmpDir;

return extract(filePath, tmpDir).then(function () {
return tmpDir;
});
try {
await extract(filePath, tmpDir);
} catch (err) {
if (err.message.startsWith('ENAMETOOLONG:')) {
// The file was probably zipped with MacOS zip utility. Which doesn't correctly set UTF-8 encoding flag.
// This causes ENAMETOOLONG error on Linux, because the resulting filename length is too long when decoded using the default string encoder.
throw new errors.UnsupportedMediaTypeError({
message: tpl(messages.invalidZipFileNameEncoding),
context: tpl(messages.invalidZipFileNameEncodingContext),
help: tpl(messages.invalidZipFileNameEncodingHelp),
code: 'INVALID_ZIP_FILE_NAME_ENCODING'
});
}
throw err;
}
return tmpDir;
}

/**
Expand Down