-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update az client to get size and url and save metadata to local json
- Loading branch information
Showing
5 changed files
with
96 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,59 @@ | ||
import express from 'express'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { FileMetadata } from '../../types/files'; | ||
import azureBlobService from '../utils/azureClient'; | ||
import { loadFilesData } from '../utils/fileUtils'; | ||
|
||
const router = express.Router(); | ||
|
||
const filesDataPath = path.join(__dirname, '../filesData.json'); | ||
// Function to list files in Azure Blob Storage | ||
const listFilesInAzureBlob = async (): Promise<FileMetadata[]> => { | ||
const files: FileMetadata[] = []; | ||
|
||
const loadFilesData = (): FileMetadata[] => { | ||
if (fs.existsSync(filesDataPath)) { | ||
const data = fs.readFileSync(filesDataPath); | ||
return JSON.parse(data.toString()) as FileMetadata[]; | ||
try { | ||
const containerClient = azureBlobService.getContainerClient(); | ||
|
||
if (containerClient) { | ||
// List all blobs in the container | ||
const blobIterator = containerClient.listBlobsFlat(); | ||
|
||
for await (const blob of blobIterator) { | ||
const fileMetadata: FileMetadata = { | ||
name: blob.name, | ||
key: blob.name, | ||
url: `${containerClient.url}/${blob.name}`, | ||
size: blob.properties.contentLength, | ||
}; | ||
|
||
files.push(fileMetadata); | ||
} | ||
} else { | ||
throw new Error('Azure Blob Service client is not initialized.'); | ||
} | ||
} catch (error) { | ||
console.error('Error retrieving files from Azure Blob Storage:', error); | ||
throw error; | ||
} | ||
return []; | ||
}; | ||
|
||
const files: FileMetadata[] = loadFilesData(); | ||
return files; | ||
}; | ||
|
||
router.get('/', (req, res) => { | ||
res.json(files); | ||
// Endpoint to get files | ||
router.get('/', async (_req, res) => { | ||
let files: FileMetadata[]; | ||
try { | ||
if (azureBlobService.getContainerClient()) { | ||
// Attempt to retrieve files from Azure Blob Storage | ||
files = await listFilesInAzureBlob(); | ||
} else { | ||
console.log('Azure Blob Service not initialized. Using local JSON backup.'); | ||
// Use local JSON backup if Azure Blob Service is not initialized | ||
files = loadFilesData(); | ||
} | ||
res.json(files); | ||
} catch (error) { | ||
console.error('Error handling request:', error); | ||
res.status(500).send('Internal Server Error'); | ||
} | ||
}); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
// Note: key and name are the same. This is an artefact of a previous AWS implementation | ||
interface FileMetadata { | ||
name: string; | ||
key: string; | ||
size?: number; | ||
url?: string; | ||
} | ||
|
||
export type { FileMetadata }; |