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 downloadContracts = async (req, res) => { |
const readContractFilesToArray = (contractsDirectoryPath) => fs |
||||
const { params: { hash: hashOrTag } } = req; |
.readdir(contractsDirectoryPath) |
||||
let directoryPath = getStorageLocation(hashOrTag); |
.then((contractFilenames) => contractFilenames |
||||
|
.map((contractFilename) => fs |
||||
if (!fs.existsSync(directoryPath)) { |
.readFile(path.join(`${contractsDirectoryPath}/${contractFilename}`), 'utf-8') |
||||
const tagsDirectory = getTagsDirectory(); |
.then((rawContractData) => JSON.parse(rawContractData)))) |
||||
|
.then((contractObjectPromises) => Promise.all([...contractObjectPromises])); |
||||
if (fs.existsSync(tagsDirectory)) { |
|
||||
const tagFilePath = path.join(tagsDirectory, hashOrTag); |
|
||||
const tagReference = fs.readFileSync(tagFilePath, 'utf-8'); |
|
||||
|
|
||||
directoryPath = getStorageLocation(tagReference); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
const contracts = []; |
const downloadContracts = async (req, res) => { |
||||
|
const { params: { hash: identifier } } = req; |
||||
fs.readdirSync(directoryPath).forEach((contractFilename) => { |
const hashBasedDirectoryPath = getStorageLocation(identifier); |
||||
const rawContractData = fs.readFileSync(path.join(`${directoryPath}/${contractFilename}`), 'utf-8'); |
|
||||
const contractJson = JSON.parse(rawContractData); |
return fs.access(hashBasedDirectoryPath) |
||||
contracts.push(contractJson); |
.then(() => readContractFilesToArray(hashBasedDirectoryPath)) |
||||
}); |
.catch(() => { |
||||
|
const tagsDirectory = getTagsDirectory(); |
||||
res.send(contracts); |
|
||||
|
return fs |
||||
|
.access(tagsDirectory) |
||||
|
.then(() => { |
||||
|
const tagFilePath = path.join(tagsDirectory, identifier); |
||||
|
|
||||
|
return fs.readFile(tagFilePath, 'utf-8') |
||||
|
.then((tagReference) => { |
||||
|
const tagBasedDirectoryPath = getStorageLocation(tagReference); |
||||
|
|
||||
|
return readContractFilesToArray(tagBasedDirectoryPath); |
||||
|
}); |
||||
|
}); |
||||
|
}).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