| 
									
										
										
										
											2017-10-31 20:18:15 -06:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | const assert = require('assert'); | 
					
						
							|  |  |  | const fs = require('fs'); | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							|  |  |  | const nconf = require('nconf'); | 
					
						
							| 
									
										
										
										
											2017-10-31 20:18:15 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | const utils = require('../src/utils'); | 
					
						
							|  |  |  | const file = require('../src/file'); | 
					
						
							| 
									
										
										
										
											2017-10-31 20:18:15 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | describe('file', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 	const filename = `${utils.generateUUID()}.png`; | 
					
						
							|  |  |  | 	const folder = 'files'; | 
					
						
							|  |  |  | 	const uploadPath = path.join(nconf.get('upload_path'), folder, filename); | 
					
						
							|  |  |  | 	const tempPath = path.join(__dirname, './files/test.png'); | 
					
						
							| 
									
										
										
										
											2017-10-31 20:18:15 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	afterEach((done) => { | 
					
						
							|  |  |  | 		fs.unlink(uploadPath, () => { | 
					
						
							| 
									
										
										
										
											2017-10-31 20:18:15 -06:00
										 |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('copyFile', () => { | 
					
						
							|  |  |  | 		it('should copy a file', (done) => { | 
					
						
							|  |  |  | 			fs.copyFile(tempPath, uploadPath, (err) => { | 
					
						
							| 
									
										
										
										
											2017-10-31 20:18:15 -06:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				assert(file.existsSync(uploadPath)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 				const srcContent = fs.readFileSync(tempPath, 'utf8'); | 
					
						
							|  |  |  | 				const destContent = fs.readFileSync(uploadPath, 'utf8'); | 
					
						
							| 
									
										
										
										
											2017-10-31 20:18:15 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				assert.strictEqual(srcContent, destContent); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should override an existing file', (done) => { | 
					
						
							| 
									
										
										
										
											2017-10-31 20:18:15 -06:00
										 |  |  | 			fs.writeFileSync(uploadPath, 'hsdkjhgkjsfhkgj'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			fs.copyFile(tempPath, uploadPath, (err) => { | 
					
						
							| 
									
										
										
										
											2017-10-31 20:18:15 -06:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				assert(file.existsSync(uploadPath)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 				const srcContent = fs.readFileSync(tempPath, 'utf8'); | 
					
						
							|  |  |  | 				const destContent = fs.readFileSync(uploadPath, 'utf8'); | 
					
						
							| 
									
										
										
										
											2017-10-31 20:18:15 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				assert.strictEqual(srcContent, destContent); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should error if source file does not exist', (done) => { | 
					
						
							|  |  |  | 			fs.copyFile(`${tempPath}0000000000`, uploadPath, (err) => { | 
					
						
							| 
									
										
										
										
											2017-10-31 20:18:15 -06:00
										 |  |  | 				assert(err); | 
					
						
							|  |  |  | 				assert.strictEqual(err.code, 'ENOENT'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should error if existing file is read only', (done) => { | 
					
						
							| 
									
										
										
										
											2017-10-31 20:18:15 -06:00
										 |  |  | 			fs.writeFileSync(uploadPath, 'hsdkjhgkjsfhkgj'); | 
					
						
							|  |  |  | 			fs.chmodSync(uploadPath, '444'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			fs.copyFile(tempPath, uploadPath, (err) => { | 
					
						
							| 
									
										
										
										
											2017-10-31 20:18:15 -06:00
										 |  |  | 				assert(err); | 
					
						
							|  |  |  | 				assert(err.code === 'EPERM' || err.code === 'EACCES'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('saveFileToLocal', () => { | 
					
						
							|  |  |  | 		it('should work', (done) => { | 
					
						
							|  |  |  | 			file.saveFileToLocal(filename, folder, tempPath, (err) => { | 
					
						
							| 
									
										
										
										
											2017-10-31 20:18:15 -06:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				assert(file.existsSync(uploadPath)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 				const oldFile = fs.readFileSync(tempPath, 'utf8'); | 
					
						
							|  |  |  | 				const newFile = fs.readFileSync(uploadPath, 'utf8'); | 
					
						
							| 
									
										
										
										
											2017-10-31 20:18:15 -06:00
										 |  |  | 				assert.strictEqual(oldFile, newFile); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should error if source does not exist', (done) => { | 
					
						
							|  |  |  | 			file.saveFileToLocal(filename, folder, `${tempPath}000000000`, (err) => { | 
					
						
							| 
									
										
										
										
											2017-10-31 20:18:15 -06:00
										 |  |  | 				assert(err); | 
					
						
							|  |  |  | 				assert.strictEqual(err.code, 'ENOENT'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2020-06-22 12:08:35 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should error if folder is relative', (done) => { | 
					
						
							|  |  |  | 			file.saveFileToLocal(filename, '../../text', `${tempPath}000000000`, (err) => { | 
					
						
							| 
									
										
										
										
											2020-06-22 12:08:35 -04:00
										 |  |  | 				assert(err); | 
					
						
							|  |  |  | 				assert.strictEqual(err.message, '[[error:invalid-path]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-10-31 20:18:15 -06:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2019-07-12 20:16:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should walk directory', (done) => { | 
					
						
							|  |  |  | 		file.walk(__dirname, (err, data) => { | 
					
						
							| 
									
										
										
										
											2019-07-12 20:16:05 -04:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert(Array.isArray(data)); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2019-09-23 22:30:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should convert mime type to extension', (done) => { | 
					
						
							| 
									
										
										
										
											2019-09-23 22:30:17 -04:00
										 |  |  | 		assert.equal(file.typeToExtension('image/png'), '.png'); | 
					
						
							|  |  |  | 		assert.equal(file.typeToExtension(''), ''); | 
					
						
							|  |  |  | 		done(); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-10-31 20:18:15 -06:00
										 |  |  | }); |