Skip to content

Commit

Permalink
import changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tjhorner committed Sep 14, 2024
1 parent 684e80e commit a643268
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
27 changes: 17 additions & 10 deletions src/tracks/track-import.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { forwardRef, Inject } from "@nestjs/common"

export interface TrackImportPayload {
filePath: string
force?: boolean
}

export const TRACK_IMPORT_QUEUE = "track-import"
Expand All @@ -31,9 +32,12 @@ export class TrackImportProcessor extends WorkerHost {
})

const hash = await this.getFileHash(job.data.filePath)
const alreadyExists = await this.tracksService.existsByFileHash(hash)
if (alreadyExists) {
throw new UnrecoverableError("File has already been processed")

if (!job.data.force) {
const alreadyExists = await this.tracksService.existsByFileHash(hash)
if (alreadyExists) {
throw new UnrecoverableError("File has already been processed")
}
}

await job.updateProgress({
Expand All @@ -42,7 +46,8 @@ export class TrackImportProcessor extends WorkerHost {

let gpxData: FeatureCollection
try {
gpxData = await this.getGpxData(job.data.filePath)
const gpxPath = await this.convertToGpx(job.data.filePath)
gpxData = await this.getGpxData(gpxPath)
} catch {
throw new UnrecoverableError("Failed to process GPX data")
}
Expand All @@ -64,7 +69,7 @@ export class TrackImportProcessor extends WorkerHost {
status: "Finalizing import",
})

const track = await this.tracksService.create({
const track = await this.tracksService.upsert({
name: path.basename(job.data.filePath),
captureDate,
filePath: job.data.filePath,
Expand All @@ -77,23 +82,25 @@ export class TrackImportProcessor extends WorkerHost {
}
}

private async getGpxData(filePath: string): Promise<FeatureCollection> {
const outPath = path.join("/tmp", path.basename(filePath))
private async convertToGpx(filePath: string): Promise<string> {
await this.runCmd("gopro2gpx", [
"--skip-dop",
"--dop-limit",
"500",
"-s",
filePath,
outPath,
filePath,
])

const gpxFile = await fs.readFile(`${outPath}.gpx`, "utf-8")
return `${filePath}.gpx`
}

private async getGpxData(gpxPath): Promise<FeatureCollection> {
const gpxFile = await fs.readFile(gpxPath, "utf-8")

const gpxData = parseGPX(gpxFile)
const featureCollection = gpxData.toGeoJSON()

await fs.unlink(`${outPath}.gpx`)
return featureCollection
}

Expand Down
9 changes: 6 additions & 3 deletions src/tracks/tracks.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get, Post, Query } from "@nestjs/common"
import { Body, Controller, Get, Post, Query } from "@nestjs/common"
import { TracksService } from "./tracks.service"

@Controller("tracks")
Expand Down Expand Up @@ -26,8 +26,11 @@ export class TracksController {
}

@Post("import")
async import(@Query("filePath") filePath: string) {
const job = await this.tracksService.startImport(filePath)
async import(
@Body("filePath") filePath: string,
@Body("force") force?: boolean,
) {
const job = await this.tracksService.startImport(filePath, force)
return job.id
}
}
22 changes: 20 additions & 2 deletions src/tracks/tracks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,36 @@ export class TracksService {
return this.tracksRepository.save(track)
}

async upsert(track: DeepPartial<Track>) {
const existing = await this.tracksRepository.findOne({
where: { filePath: track.filePath },
})

if (existing) {
return this.tracksRepository.save({
...existing,
...track,
})
}

return this.tracksRepository.save(track)
}

async existsByFileHash(fileHash: string): Promise<boolean> {
const count = await this.tracksRepository.count({ where: { fileHash } })
return count > 0
}

async startImport(filePath: string) {
async startImport(filePath: string, force: boolean = false) {
const exists = fs.existsSync(filePath)
if (!exists) {
throw new Error(`File not found: ${filePath}`)
}

return this.trackImportQueue.add(path.basename(filePath), { filePath })
return this.trackImportQueue.add(path.basename(filePath), {
filePath,
force,
})
}

toGeoJSON(tracks: Track[]) {
Expand Down

0 comments on commit a643268

Please sign in to comment.