-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
fix(plex): Refactor plex webhook formdata parsing to be more defensive
- Loading branch information
Showing
2 changed files
with
109 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Files, File } from "formidable"; | ||
import VolatileFile from "formidable/VolatileFile.js"; | ||
|
||
// typings from Formidable are all nuts. | ||
// VolatileFile is missing buffer and also does not extend File even though it should | ||
|
||
export const getValidMultipartJsonFile = (files: Files | File): [VolatileFile, string[]?] => { | ||
|
||
const logs: string[] = []; | ||
|
||
try { | ||
|
||
if (isVolatileFile(files)) { | ||
if ('mimetype' in files && files.mimetype !== undefined) { | ||
if (files.mimetype.includes('json')) { | ||
logs.push(`Found ${getFileIdentifier(files)} with mimetype '${files.mimetype}'`) | ||
return [files as unknown as VolatileFile, logs]; | ||
} else { | ||
logs.push(`${getFileIdentifier(files)} mimetype '${files.mimetype}' does not include 'json'`); | ||
} | ||
} else { | ||
logs.push(`${getFileIdentifier(files)} had no mimetype`) | ||
} | ||
} else { | ||
for (const [partName, namedFile] of Object.entries(files)) { | ||
if (Array.isArray(namedFile)) { | ||
for (const [index, file] of Object.entries(namedFile)) { | ||
if ('mimetype' in file && file.mimetype !== undefined) { | ||
if (file.mimetype.includes('json')) { | ||
logs.push(`Found ${partName}.${index}.${getFileIdentifier(file)} with mimetype '${file.mimetype}'`) | ||
return [file as unknown as VolatileFile, logs]; | ||
} else { | ||
logs.push(`${partName}.${index}.${getFileIdentifier(file)} mimetype '${file.mimetype}' does not include 'json'`); | ||
} | ||
} else { | ||
logs.push(`${partName}.${index}.${getFileIdentifier(file)} had no mimetype`) | ||
} | ||
} | ||
} else { | ||
// this shouldn't happen but it was happening so... | ||
const singleFile = namedFile as File; | ||
if (typeof singleFile === 'object' && 'mimetype' in singleFile && singleFile.mimetype !== undefined) { | ||
if (singleFile.mimetype.includes('json')) { | ||
logs.push(`Found ${partName}.${getFileIdentifier(singleFile)} with mimetype '${singleFile.mimetype}'`); | ||
return [namedFile as unknown as VolatileFile, logs]; | ||
} else { | ||
logs.push(`${partName}.${getFileIdentifier(singleFile)} mimetype '${singleFile.mimetype}' does not include 'json'`); | ||
} | ||
} else { | ||
logs.push(`${partName}.${getFileIdentifier(singleFile)} had no mimetype`) | ||
} | ||
} | ||
} | ||
} | ||
} catch (e) { | ||
throw new Error('Unexpected error occurred while trying to find valid json file in formdata', {cause: e}); | ||
} | ||
|
||
return [undefined, logs]; | ||
} | ||
|
||
const isVolatileFile = (val: unknown): val is File => { | ||
return typeof val === 'object' | ||
&& val !== null | ||
&& 'size' in val | ||
&& 'filepath' in val; | ||
} | ||
|
||
export const getFileIdentifier = (f: File): string => { | ||
return f.originalFilename === null ? f.newFilename : f.originalFilename; | ||
} |