|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import { logger } from 'kv-logger'; |
| 4 | +import _ from 'lodash'; |
| 5 | +import { AppError } from '../app-error'; |
| 6 | +import { config } from '../config'; |
| 7 | +import { createEmptyFolder, copy } from '../utils/common'; |
| 8 | +import { calcAllFileSha256, packageHashSync } from '../utils/security'; |
| 9 | + |
| 10 | +const manifestFilename = 'manifest.json'; |
| 11 | +const contentsName = 'contents'; |
| 12 | + |
| 13 | +class DataCenterManager { |
| 14 | + getDataDir() { |
| 15 | + return config.common.dataDir; |
| 16 | + } |
| 17 | + |
| 18 | + hasPackageStoreSync(packageHash: string) { |
| 19 | + const dataDir = this.getDataDir(); |
| 20 | + const packageHashPath = path.join(dataDir, packageHash); |
| 21 | + const manifestFile = path.join(packageHashPath, manifestFilename); |
| 22 | + const contentPath = path.join(packageHashPath, contentsName); |
| 23 | + return fs.existsSync(manifestFile) && fs.existsSync(contentPath); |
| 24 | + } |
| 25 | + |
| 26 | + getPackageInfo(packageHash: string) { |
| 27 | + if (this.hasPackageStoreSync(packageHash)) { |
| 28 | + const dataDir = this.getDataDir(); |
| 29 | + const packageHashPath = path.join(dataDir, packageHash); |
| 30 | + const manifestFile = path.join(packageHashPath, manifestFilename); |
| 31 | + const contentPath = path.join(packageHashPath, contentsName); |
| 32 | + return this.buildPackageInfo(packageHash, packageHashPath, contentPath, manifestFile); |
| 33 | + } |
| 34 | + throw new AppError("can't get PackageInfo"); |
| 35 | + } |
| 36 | + |
| 37 | + buildPackageInfo( |
| 38 | + packageHash: string, |
| 39 | + packageHashPath: string, |
| 40 | + contentPath: string, |
| 41 | + manifestFile: string, |
| 42 | + ) { |
| 43 | + return { |
| 44 | + packageHash, |
| 45 | + path: packageHashPath, |
| 46 | + contentPath, |
| 47 | + manifestFilePath: manifestFile, |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + validateStore(providePackageHash: string) { |
| 52 | + const dataDir = this.getDataDir(); |
| 53 | + const packageHashPath = path.join(dataDir, providePackageHash); |
| 54 | + const manifestFile = path.join(packageHashPath, manifestFilename); |
| 55 | + const contentPath = path.join(packageHashPath, contentsName); |
| 56 | + if (!this.hasPackageStoreSync(providePackageHash)) { |
| 57 | + logger.debug(`validateStore providePackageHash not exist`); |
| 58 | + return Promise.resolve(false); |
| 59 | + } |
| 60 | + return calcAllFileSha256(contentPath).then((manifestJson) => { |
| 61 | + const packageHash = packageHashSync(manifestJson); |
| 62 | + let manifestJsonLocal; |
| 63 | + try { |
| 64 | + manifestJsonLocal = JSON.parse(fs.readFileSync(manifestFile, { encoding: 'utf8' })); |
| 65 | + } catch (e) { |
| 66 | + logger.debug(`validateStore manifestFile contents invilad`); |
| 67 | + return false; |
| 68 | + } |
| 69 | + const packageHashLocal = packageHashSync(manifestJsonLocal); |
| 70 | + if ( |
| 71 | + _.eq(providePackageHash, packageHash) && |
| 72 | + _.eq(providePackageHash, packageHashLocal) |
| 73 | + ) { |
| 74 | + logger.debug(`validateStore store files is ok`); |
| 75 | + return true; |
| 76 | + } |
| 77 | + logger.debug(`validateStore store files broken`); |
| 78 | + return false; |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + storePackage(sourceDst: string, force = false) { |
| 83 | + return calcAllFileSha256(sourceDst).then((manifestJson) => { |
| 84 | + const packageHash = packageHashSync(manifestJson); |
| 85 | + const dataDir = this.getDataDir(); |
| 86 | + const packageHashPath = path.join(dataDir, packageHash); |
| 87 | + const manifestFile = path.join(packageHashPath, manifestFilename); |
| 88 | + const contentPath = path.join(packageHashPath, contentsName); |
| 89 | + return this.validateStore(packageHash).then((isValidate) => { |
| 90 | + if (!force && isValidate) { |
| 91 | + return this.buildPackageInfo( |
| 92 | + packageHash, |
| 93 | + packageHashPath, |
| 94 | + contentPath, |
| 95 | + manifestFile, |
| 96 | + ); |
| 97 | + } |
| 98 | + return createEmptyFolder(packageHashPath).then(() => { |
| 99 | + return copy(sourceDst, contentPath).then(() => { |
| 100 | + const manifestString = JSON.stringify(manifestJson); |
| 101 | + fs.writeFileSync(manifestFile, manifestString); |
| 102 | + return this.buildPackageInfo( |
| 103 | + packageHash, |
| 104 | + packageHashPath, |
| 105 | + contentPath, |
| 106 | + manifestFile, |
| 107 | + ); |
| 108 | + }); |
| 109 | + }); |
| 110 | + }); |
| 111 | + }); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +export const dataCenterManager = new DataCenterManager(); |
0 commit comments