Skip to content

Commit

Permalink
fix: make sure local_content_hash exists
Browse files Browse the repository at this point in the history
  • Loading branch information
cazala committed May 6, 2022
1 parent 8ba2f87 commit 6158f3e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ coverage

.DS_Store
backup.dump
backup*.dump
backup*.dump
.env.migration*
21 changes: 15 additions & 6 deletions scripts/updateUnsyncedItemHashes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export async function run() {

console.log('DB: Connected!')

console.log('Connected to bucket:', s3.getBucket())
console.log('Access key:', s3.getAccessKey())

try {
// Objective get all published items that are
// de-synced with the catalyst (or that are not in the catalyst)
Expand Down Expand Up @@ -146,16 +149,17 @@ function needsMigration(item: FullItem) {
}

async function migrateItem(item: FullItem) {
const files: Record<string, Buffer> = {}

console.log('Downloading files...')
const files: Record<string, Buffer> = {}
const filePromises: Promise<any>[] = []
for (const path in item.contents) {
const hash = item.contents[path]
const promise = s3.readFile(hash).then((file) => {
if (file && file.Body) {
const buffer = file.Body
files[path] = buffer as Buffer
} else {
console.error('File not found or body is undefined', file)
}
})
filePromises.push(promise)
Expand All @@ -167,9 +171,12 @@ async function migrateItem(item: FullItem) {
const newContent: Record<string, string> = {}
const hashPromises: Promise<any>[] = []
for (const path in item.contents) {
const promise = hashV1(files[path]).then(
(hash) => (newContent[path] = hash)
)
const file = files[path]
if (!file) {
console.error('file not found for path', path)
throw new Error('File not found')
}
const promise = hashV1(file).then((hash) => (newContent[path] = hash))
hashPromises.push(promise)
}
await Promise.all(hashPromises)
Expand Down Expand Up @@ -225,7 +232,9 @@ async function migrateStandardItem(
attributes.blockchain_item_id = item ? item.blockchain_item_id : null
// Compute the content hash of the item to later store it in the DB
attributes.local_content_hash =
collection && isStandardItemPublished(attributes, collection)
item.local_content_hash &&
collection &&
isStandardItemPublished(attributes, collection)
? await calculateItemContentHash(attributes, collection)
: null
return Item.upsert(attributes)
Expand Down
10 changes: 10 additions & 0 deletions src/S3/S3Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
uploadFile,
ACLValues,
copyFile,
getBucket,
getAccessKey,
} from './s3'
import { S3Type } from './types'

Expand All @@ -18,6 +20,14 @@ export class S3Model {
this.type = type
}

getBucket() {
return getBucket()
}

getAccessKey() {
return getAccessKey()
}

async readFileBody(filename: string) {
const file = await this.readFile(filename)
let body
Expand Down
8 changes: 8 additions & 0 deletions src/S3/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ if (STORAGE_URL) {

export const s3 = new AWS.S3(config)

export function getBucket() {
return BUCKET_NAME
}

export function getAccessKey() {
return ACCESS_KEY
}

export function readFile(key: string): Promise<AWS.S3.GetObjectOutput> {
const params = {
Bucket: BUCKET_NAME,
Expand Down

0 comments on commit 6158f3e

Please sign in to comment.