forked from laurent22/joplin
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Server: Fixed S3 storage connection and improved connectiob checks
- Loading branch information
Showing
4 changed files
with
53 additions
and
31 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
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { DbConnection } from '../db'; | ||
import { Models } from '../models/factory'; | ||
import loadStorageDriver from '../models/items/storage/loadStorageDriver'; | ||
import parseStorageConnectionString from '../models/items/storage/parseStorageConnectionString'; | ||
import { Context } from '../models/items/storage/StorageDriverBase'; | ||
import { StorageDriverConfig, StorageDriverType } from './types'; | ||
import uuidgen from './uuidgen'; | ||
|
||
export default async function(connection: string | StorageDriverConfig, db: DbConnection, models: Models): Promise<string> { | ||
const storageConfig = typeof connection === 'string' ? parseStorageConnectionString(connection) : connection; | ||
|
||
if (storageConfig.type === StorageDriverType.Database) return 'Database storage is special and cannot be checked this way. If the connection to the database was successful then the storage driver should work too.'; | ||
|
||
const driver = await loadStorageDriver(storageConfig, db, { assignDriverId: false }); | ||
const itemId = `testingconnection${uuidgen(8)}`; | ||
const itemContent = Buffer.from(uuidgen(8)); | ||
const context: Context = { models }; | ||
|
||
try { | ||
await driver.write(itemId, itemContent, context); | ||
} catch (error) { | ||
error.message = `Could not write content to storage: ${error.message}`; | ||
throw error; | ||
} | ||
|
||
if (!(await driver.exists(itemId, context))) { | ||
throw new Error(`Written item does not exist: ${itemId}`); | ||
} | ||
|
||
const readContent = await driver.read(itemId, context); | ||
if (readContent.toString() !== itemContent.toString()) throw new Error(`Could not read back written item. Expected: ${itemContent.toString()}. Got: ${readContent.toString()}`); | ||
|
||
await driver.delete(itemId, context); | ||
|
||
if (await driver.exists(itemId, context)) { | ||
throw new Error(`Deleted item still exist: ${itemId}`); | ||
} | ||
|
||
return 'Item was written, read back and deleted without any error.'; | ||
} |