feat(metadata): implement MetadataRequest and supporting classes for file metadata extraction and response formatting
This commit is contained in:
		
							parent
							
								
									72769a3ef4
								
							
						
					
					
						commit
						74fb118aa4
					
				
					 4 changed files with 126 additions and 0 deletions
				
			
		
							
								
								
									
										24
									
								
								scripts/API/actions/metadata.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								scripts/API/actions/metadata.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,24 @@ | ||||||
|  | const CustomErrors = require(`../../utilities/errors`); | ||||||
|  | const Messaging = require(`../../utilities/messaging`); | ||||||
|  | 
 | ||||||
|  | const MetadataExtractor = require(`../../file-management/analyze`); | ||||||
|  | const Cleaner = require(`../response/simple-data`); | ||||||
|  | 
 | ||||||
|  | const MetadataRequest = class Request { | ||||||
|  | 	static analyze (REQUEST, RESPONSE) { | ||||||
|  | 		// Check if the file is valid.
 | ||||||
|  | 		if (REQUEST.file) { | ||||||
|  | 			// Get the file information.
 | ||||||
|  | 			const fileInfo = new MetadataExtractor(REQUEST.file).extract([ `originalname`, `mimetype`, `size`]); | ||||||
|  | 			 | ||||||
|  | 			const cleanedData = (new Cleaner(fileInfo)).format(); | ||||||
|  | 			RESPONSE.status(200).json(cleanedData); | ||||||
|  | 		} else { | ||||||
|  | 			// If the file is not valid, return an error.
 | ||||||
|  | 			const ERROR = new CustomErrors.Data.Missing(REQUEST.file); | ||||||
|  | 			Messaging.exception(RESPONSE, ERROR); | ||||||
|  | 		}; | ||||||
|  | 	}; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | module.exports = MetadataRequest; | ||||||
							
								
								
									
										49
									
								
								scripts/API/response/parser.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								scripts/API/response/parser.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,49 @@ | ||||||
|  | /*  | ||||||
|  | 	Modify data returned by "middle-man" scripts.  | ||||||
|  | */ | ||||||
|  | const Parser = class { | ||||||
|  | 	constructor(DATA = {}, MAPPINGS = {}) { | ||||||
|  | 		this.data = DATA; | ||||||
|  | 		this.mappings = MAPPINGS; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	/* | ||||||
|  | 		Modify the data for output.  | ||||||
|  | 
 | ||||||
|  | 		@return {Object} cleaned data | ||||||
|  | 	*/ | ||||||
|  | 	format () { | ||||||
|  | 		let ENTRIES; | ||||||
|  | 		/*  | ||||||
|  | 			Map certain new keys to their original counterparts.  | ||||||
|  | 		*/ | ||||||
|  | 		function mapKeys (ORIGINAL = {}, MAPPINGS) { | ||||||
|  | 			let RESULT = {}; | ||||||
|  | 			 | ||||||
|  | 			Object.entries(MAPPINGS).forEach(([NEW, OLD]) => { | ||||||
|  | 				RESULT[NEW] = (ORIGINAL[OLD] != null ? (typeof ORIGINAL[OLD]).includes(`str`) : false) ? ORIGINAL[OLD].trim() : ORIGINAL[OLD]; | ||||||
|  | 			}); | ||||||
|  | 			 | ||||||
|  | 			return RESULT; | ||||||
|  | 		} | ||||||
|  | 		 | ||||||
|  | 		if (this.data instanceof Array) { | ||||||
|  | 			ENTRIES = []; | ||||||
|  | 	 | ||||||
|  | 			(this.data.length) ? this.data.forEach((SELECTED) => { | ||||||
|  | 				if (SELECTED) { | ||||||
|  | 					let CLEANED = mapKeys(SELECTED, this.mappings); | ||||||
|  | 					(Object.keys(CLEANED).length) ? ENTRIES.push(CLEANED) : false; | ||||||
|  | 				}; | ||||||
|  | 			}) : false; | ||||||
|  | 		} else if (this.data && this.data instanceof Object) { | ||||||
|  | 			ENTRIES = mapKeys(this.data, this.mappings); | ||||||
|  | 		}; | ||||||
|  | 
 | ||||||
|  | 		this.cleaned = ENTRIES; | ||||||
|  | 		return (this.cleaned); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | module.exports = Parser; | ||||||
							
								
								
									
										18
									
								
								scripts/API/response/simple-data.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								scripts/API/response/simple-data.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,18 @@ | ||||||
|  | const Parser = require(`./parser`); | ||||||
|  | 
 | ||||||
|  | class SimpleMetadata extends Parser { | ||||||
|  | 	/* | ||||||
|  | 		Clean the log information.  | ||||||
|  | 	 | ||||||
|  | 		@param {Object} ENTRY the selected entry, or the entries | ||||||
|  | 	*/ | ||||||
|  | 	constructor(ENTRY) { | ||||||
|  | 		super(ENTRY, { | ||||||
|  | 			"name": "originalname", | ||||||
|  | 			"type": "mimetype", | ||||||
|  | 			"size": "size" | ||||||
|  | 		}); | ||||||
|  | 	}; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | module.exports = SimpleMetadata; | ||||||
							
								
								
									
										35
									
								
								scripts/file-management/analyze.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								scripts/file-management/analyze.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,35 @@ | ||||||
|  | const MetadataExtractor = class { | ||||||
|  | 	/*  | ||||||
|  | 	 * MetadataExtractor class | ||||||
|  | 	 * This class is used to extract metadata from a BLOB object. | ||||||
|  | 	*/ | ||||||
|  | 	constructor(BLOB) { | ||||||
|  | 		this.blob = BLOB; | ||||||
|  | 
 | ||||||
|  | 		this.extract([`fieldname`, `encoding`, `originalname`, `mimetype`, `size`]); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	/*  | ||||||
|  | 	 * Extract data from the blob.  | ||||||
|  | 	*/ | ||||||
|  | 	extract(FIELDS) { | ||||||
|  | 		// Verify the validity of the blob.
 | ||||||
|  | 		if (!this.blob) { | ||||||
|  | 			throw new Error("Invalid BLOB object"); | ||||||
|  | 		}; | ||||||
|  | 
 | ||||||
|  | 		// Extract the relevant fields. 
 | ||||||
|  | 		let RESPONSE = {}; | ||||||
|  | 		FIELDS.forEach((FIELD) => { | ||||||
|  | 			if (!(this.blob[FIELD] === undefined)) { | ||||||
|  | 				this[FIELD] = this.blob[FIELD]; | ||||||
|  | 				RESPONSE[FIELD] = this.blob[FIELD]; | ||||||
|  | 			} | ||||||
|  | 		}); | ||||||
|  | 
 | ||||||
|  | 		// Return the response.
 | ||||||
|  | 		return (RESPONSE); | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | module.exports = MetadataExtractor; | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue