mirror of https://gitlab.com/ecentrics/concordia
Apostolos Fanakis
4 years ago
6 changed files with 53 additions and 44 deletions
@ -1,31 +1,38 @@ |
|||||
import * as fs from 'fs'; |
import { promises as fs } from 'fs'; |
||||
import path from 'path'; |
import path from 'path'; |
||||
import { getStorageLocation, getTagsDirectory } from '../utils/storageUtils'; |
import { getStorageLocation, getTagsDirectory } from '../utils/storageUtils'; |
||||
|
|
||||
|
const readContractFilesToArray = (contractsDirectoryPath) => fs |
||||
|
.readdir(contractsDirectoryPath) |
||||
|
.then((contractFilenames) => contractFilenames |
||||
|
.map((contractFilename) => fs |
||||
|
.readFile(path.join(`${contractsDirectoryPath}/${contractFilename}`), 'utf-8') |
||||
|
.then((rawContractData) => JSON.parse(rawContractData)))) |
||||
|
.then((contractObjectPromises) => Promise.all([...contractObjectPromises])); |
||||
|
|
||||
const downloadContracts = async (req, res) => { |
const downloadContracts = async (req, res) => { |
||||
const { params: { hash: hashOrTag } } = req; |
const { params: { hash: identifier } } = req; |
||||
let directoryPath = getStorageLocation(hashOrTag); |
const hashBasedDirectoryPath = getStorageLocation(identifier); |
||||
|
|
||||
if (!fs.existsSync(directoryPath)) { |
return fs.access(hashBasedDirectoryPath) |
||||
|
.then(() => readContractFilesToArray(hashBasedDirectoryPath)) |
||||
|
.catch(() => { |
||||
const tagsDirectory = getTagsDirectory(); |
const tagsDirectory = getTagsDirectory(); |
||||
|
|
||||
if (fs.existsSync(tagsDirectory)) { |
return fs |
||||
const tagFilePath = path.join(tagsDirectory, hashOrTag); |
.access(tagsDirectory) |
||||
const tagReference = fs.readFileSync(tagFilePath, 'utf-8'); |
.then(() => { |
||||
|
const tagFilePath = path.join(tagsDirectory, identifier); |
||||
directoryPath = getStorageLocation(tagReference); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
const contracts = []; |
return fs.readFile(tagFilePath, 'utf-8') |
||||
|
.then((tagReference) => { |
||||
|
const tagBasedDirectoryPath = getStorageLocation(tagReference); |
||||
|
|
||||
fs.readdirSync(directoryPath).forEach((contractFilename) => { |
return readContractFilesToArray(tagBasedDirectoryPath); |
||||
const rawContractData = fs.readFileSync(path.join(`${directoryPath}/${contractFilename}`), 'utf-8'); |
|
||||
const contractJson = JSON.parse(rawContractData); |
|
||||
contracts.push(contractJson); |
|
||||
}); |
}); |
||||
|
}); |
||||
res.send(contracts); |
}).then((contracts) => res.send(contracts)) |
||||
|
.catch(() => Promise.reject(new Error(`No contracts version or tag found for ${identifier}.`))); |
||||
}; |
}; |
||||
|
|
||||
export default downloadContracts; |
export default downloadContracts; |
||||
|
Loading…
Reference in new issue