Skip to content

Commit

Permalink
fix some importing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pacoccino committed Feb 24, 2022
1 parent aa59a26 commit 388aa0c
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 19 deletions.
5 changes: 3 additions & 2 deletions api/src/lib/images/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ export type ImageMetadata = {
}

export async function getMetadata(
path: Buffer | string
path: Buffer | string,
fileType: string
): Promise<ImageMetadata> {
const rawMD = await readMetadata_exifr(path)
const rawMD = await readMetadata_exifr(path, fileType)
const parsedMD = parseMetadata_exifr(rawMD)
return {
raw: rawMD,
Expand Down
31 changes: 23 additions & 8 deletions api/src/lib/images/metadata_parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import moment from 'moment'
import _ from 'lodash'
import Decimal from 'decimal.js'
import { parse } from 'exifr'

type ImageRawMetadata = Record<string, Record<string, any>>

Expand Down Expand Up @@ -35,22 +35,37 @@ export type ImageParsedMetadata = {
keywords?: string[]
}

function getProp(
object: Record<string, any> | null | undefined,
propPaths: string[] | string
): any | null {
if (typeof propPaths === 'string') propPaths = [propPaths]
for (const i in propPaths) {
const propPath = propPaths[i]
const val = _.get(object, propPath)
if (val) return val
}
return null
}

export function parseMetadata_exifr(
rawMD: ImageRawMetadata
): ImageParsedMetadata {
const parsed: ImageParsedMetadata = {}

// dates
if (rawMD.exif?.CreateDate) {
const captureDate = getProp(rawMD, [
'exif.CreateDate',
'exif.DateTimeOriginal',
])
if (captureDate) {
parsed.date = {
capture: moment
.utc(rawMD.exif.CreateDate, 'YYYY-MM-DD hh:mm:ss')
.toDate(),
capture: moment.utc(captureDate, 'YYYY-MM-DD hh:mm:ss').toDate(),
}
}

// keywords
const keywords = rawMD.iptc?.Keywords
const keywords = getProp(rawMD, ['iptc.Keywords'])
if (keywords) {
if (typeof keywords === 'string') parsed.keywords = [keywords]
else parsed.keywords = keywords
Expand Down Expand Up @@ -144,10 +159,10 @@ export function parseMetadata_exifr(
}

function hasAll(obj: any, props: string[]) {
return obj && props.reduce((acc, curr) => acc && !!obj[curr], true)
return obj && props.reduce((acc, prop) => acc && !!_.has(obj, prop), true)
}
function hasSome(obj: any, props: string[]) {
return obj && props.reduce((acc, curr) => acc || !!obj[curr], false)
return obj && props.reduce((acc, prop) => acc || !!_.has(obj, prop), false)
}

export function getDMS2DD(l, ref) {
Expand Down
16 changes: 11 additions & 5 deletions api/src/lib/images/metadata_reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ const exifrOptions = {
mergeOutput: false,
}

const SUPPORTED_FILE_TYPES = ['jpg', 'tif', 'heic', 'avif', 'PNG']

export async function readMetadata_exifr(
path: Buffer | string
path: Buffer | string,
fileType: string
): Promise<ImageRawMetadata> {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const rawMD = await exifr.parse(path, exifrOptions)
return sanitize(rawMD)
if (SUPPORTED_FILE_TYPES.includes(fileType)) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const rawMD = await exifr.parse(path, exifrOptions)
return sanitize(rawMD || {})
}
return {}
}

function sanitize(j: any): any {
Expand Down
5 changes: 3 additions & 2 deletions api/src/lib/importer/importFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ export const getImportWorker = ({ logger, prefix, rootDir }) =>
const buffer = await fd.readFile()
const fileType = await ft.fromBuffer(buffer)

if (isFileTypeExcluded(fileType.ext)) return TaskResult.EXCLUDED
if (!fileType || isFileTypeExcluded(fileType.ext))
return TaskResult.EXCLUDED

const imageMetadata = await getMetadata(buffer)
const imageMetadata = await getMetadata(buffer, fileType.ext)
if (!imageMetadata.parsed.date) {
return TaskResult.NO_DATE
}
Expand Down
1 change: 1 addition & 0 deletions api/src/lib/importer/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export async function reportErrors(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
errorCode: e.error.code,
errorStack: e.error.stack,
}))

await addLog('./logs/importErrors.json', es, logIndex)
Expand Down
2 changes: 1 addition & 1 deletion api/src/lib/importer/supportedFiles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fpath from 'path'

export const ACCEPTED_EXTENSIONS = ['jpg', 'jpg', 'png', 'webp', 'tif']
export const ACCEPTED_EXTENSIONS = ['jpg', 'png', 'webp', 'tif']
export const EXCLUDED_FILES = ['.DS_Store', 'Icon', 'Thumbs.db']

export function isPathExcluded(path: string) {
Expand Down
1 change: 0 additions & 1 deletion api/src/lib/importer/tagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export async function createImageTags(
create: tagGroup_keywordsInput,
})

console.log(imageMetadata.parsed.keywords)
for (const i in imageMetadata.parsed.keywords) {
const keyword = imageMetadata.parsed.keywords[i]
const tag_keywordInput = {
Expand Down

0 comments on commit 388aa0c

Please sign in to comment.