You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			37 lines
		
	
	
		
			1019 B
		
	
	
	
		
			TypeScript
		
	
		
		
			
		
	
	
			37 lines
		
	
	
		
			1019 B
		
	
	
	
		
			TypeScript
		
	
| 
								 
											4 years ago
										 
									 | 
							
								//      createDeleter :: AttachmentsPath ->
							 | 
						||
| 
								 | 
							
								//                       RelativePath ->
							 | 
						||
| 
								 | 
							
								//                       IO Unit
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import { isString } from 'lodash';
							 | 
						||
| 
								 | 
							
								import path from 'path';
							 | 
						||
| 
								 | 
							
								import fse from 'fs-extra';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								export const createDeleter = (root: string) => {
							 | 
						||
| 
								 | 
							
								  if (!isString(root)) {
							 | 
						||
| 
								 | 
							
								    throw new TypeError("'root' must be a path");
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  return async (relativePath: string) => {
							 | 
						||
| 
								 | 
							
								    if (!isString(relativePath)) {
							 | 
						||
| 
								 | 
							
								      throw new TypeError("'relativePath' must be a string");
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    const absolutePath = path.join(root, relativePath);
							 | 
						||
| 
								 | 
							
								    const normalized = path.normalize(absolutePath);
							 | 
						||
| 
								 | 
							
								    if (!normalized.startsWith(root)) {
							 | 
						||
| 
								 | 
							
								      throw new Error('Invalid relative path');
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    await fse.remove(absolutePath);
							 | 
						||
| 
								 | 
							
								  };
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								const PATH = 'attachments.noindex';
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								//      getPath :: AbsolutePath -> AbsolutePath
							 | 
						||
| 
								 | 
							
								export const getAttachmentsPath = (userDataPath: string) => {
							 | 
						||
| 
								 | 
							
								  if (!isString(userDataPath)) {
							 | 
						||
| 
								 | 
							
								    throw new TypeError("'userDataPath' must be a string");
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								  return path.join(userDataPath, PATH);
							 | 
						||
| 
								 | 
							
								};
							 |