Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions src/@types/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ import type {
ComputeResourceRequest,
DBComputeJobMetadata
} from './C2D/C2D.js'
import {
ArweaveFileObject,
FileObjectType,
EncryptMethod,
FtpFileObject,
IpfsFileObject,
UrlFileObject,
BaseFileObject
} from './fileObject'
import { FileObjectType, StorageObject, EncryptMethod } from './fileObject'

export interface Command {
command: string // command name
Expand Down Expand Up @@ -69,7 +61,7 @@ export interface FileInfoCommand extends Command {
did?: string
serviceId?: string
fileIndex?: number
file?: UrlFileObject | ArweaveFileObject | IpfsFileObject | FtpFileObject
file?: StorageObject
checksum?: boolean
}
// group these 2
Expand Down Expand Up @@ -135,7 +127,7 @@ export interface EncryptFileCommand extends Command {
consumerAddress: string
signature: string
encryptionType?: EncryptMethod.AES | EncryptMethod.ECIES
files?: BaseFileObject
files?: StorageObject
rawData?: Buffer
policyServer?: any // object to pass to policy server
}
Expand Down
15 changes: 7 additions & 8 deletions src/@types/fileObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,10 @@ export interface FileInfoResponse {
encryptMethod?: EncryptMethod
}

export interface FileInfoHttpRequest {
type?: FileObjectType
did?: string
hash?: string
url?: string
transactionId?: string
serviceId?: string
}
export type FileInfoHttpRequest =
| StorageObject
| {
did?: string
serviceId?: string
checksum?: boolean
}
13 changes: 12 additions & 1 deletion src/components/core/handler/fileInfoHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Readable } from 'stream'
import { P2PCommandResponse } from '../../../@types/index.js'
import { StorageObject } from '../../../@types/fileObject.js'
import { FileObjectType, StorageObject } from '../../../@types/fileObject.js'
import { OceanNodeConfig } from '../../../@types/OceanNode.js'
import { FileInfoCommand } from '../../../@types/commands.js'
import { CORE_LOGGER } from '../../../utils/logging/common.js'
Expand Down Expand Up @@ -54,6 +54,17 @@ export class FileInfoHandler extends CommandHandler {
return buildInvalidRequestMessage('Invalid Request: no fields are present!')
}
}

const matchesRegex = (value: string, regex: RegExp): boolean => regex.test(value)
if (command.did && !matchesRegex(command.did, /^did:op/)) {
return buildInvalidRequestMessage('Invalid Request: invalid did!')
}
if (command.type && !Object.values(FileObjectType).includes(command.type)) {
return buildInvalidRequestMessage(
'Invalid Request: type must be one of ' + Object.values(FileObjectType).join(', ')
)
}

return validation
}

Expand Down
89 changes: 19 additions & 70 deletions src/components/httpRoutes/fileInfo.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import express, { Request, Response } from 'express'
import {
ArweaveFileObject,
FileInfoHttpRequest,
FileObjectType,
FtpFileObject,
IpfsFileObject,
UrlFileObject
StorageObject
} from '../../@types/fileObject.js'
import { PROTOCOL_COMMANDS, SERVICES_API_BASE_PATH } from '../../utils/constants.js'
import { FileInfoHandler } from '../core/handler/fileInfoHandler.js'
Expand All @@ -16,96 +13,48 @@ export const fileInfoRoute = express.Router()
fileInfoRoute.use(express.json()) // Ensure JSON parsing middleware is used

// Validation function
const validateFileInfoRequest = (req: FileInfoHttpRequest): boolean => {
// Helper function to check if a string matches a regular expression
const matchesRegex = (value: string, regex: RegExp): boolean => regex.test(value)

if (!req.type && !req.did) return false // either 'type' or 'did' is required
if (req.type && !['ipfs', 'url', 'arweave', 's3', 'ftp'].includes(req.type)) {
return false // 'type' must be one of the allowed values
}
if (req.did && !matchesRegex(req.did, /^did:op/)) return false // 'did' must match the regex
if (req.type === 'ipfs' && !req.hash) return false // 'hash' is required if 'type' is 'ipfs'
if (req.type === 'url' && !req.url) return false // 'url' is required if 'type' is 'url'
if (req.type === 'ftp' && !req.url) return false // 'url' is required if 'type' is 'ftp'
if (req.type === 'arweave' && !req.transactionId) return false // 'transactionId' is required if 'type' is 'arweave'
if (!req.type && !req.serviceId) return false // 'serviceId' is required if 'type' is not provided

return true
}

fileInfoRoute.post(
`${SERVICES_API_BASE_PATH}/fileInfo`,
express.urlencoded({ extended: true, type: '*/*' }),
async (req: Request, res: Response): Promise<void> => {
const fileInfoReq: FileInfoHttpRequest = req.body as unknown as FileInfoHttpRequest
HTTP_LOGGER.logMessage(`FileInfo request received: ${JSON.stringify(req.body)}`, true)

if (!validateFileInfoRequest(fileInfoReq)) {
res.status(400).send('Invalid request parameters')
return
}

try {
const hasType =
'type' in fileInfoReq &&
fileInfoReq.type != null &&
String(fileInfoReq.type).trim() !== ''
const hasDid =
'did' in fileInfoReq &&
fileInfoReq.did != null &&
String(fileInfoReq.did).trim() !== ''
if (!hasType && !hasDid) {
res.status(400).send('Invalid request parameters')
return
}
// Retrieve the file info
let fileObject: UrlFileObject | IpfsFileObject | ArweaveFileObject | FtpFileObject
let fileObject: StorageObject
let fileInfoTask: FileInfoCommand

if (fileInfoReq.did && fileInfoReq.serviceId) {
if (`did` in fileInfoReq && fileInfoReq.did && fileInfoReq.serviceId) {
fileInfoTask = {
command: PROTOCOL_COMMANDS.FILE_INFO,
did: fileInfoReq.did,
serviceId: fileInfoReq.serviceId,
caller: req.caller
}
} else if (fileInfoReq.type === 'url' && fileInfoReq.url) {
fileObject = {
type: 'url',
url: fileInfoReq.url,
method: 'GET'
} as UrlFileObject
fileInfoTask = {
command: PROTOCOL_COMMANDS.FILE_INFO,
file: fileObject,
type: fileObject.type as FileObjectType,
caller: req.caller
}
} else if (fileInfoReq.type === 'ipfs' && fileInfoReq.hash) {
fileObject = {
type: 'ipfs',
hash: fileInfoReq.hash,
method: 'GET'
} as IpfsFileObject
fileInfoTask = {
command: PROTOCOL_COMMANDS.FILE_INFO,
file: fileObject,
type: fileObject.type as FileObjectType,
caller: req.caller
}
} else if (fileInfoReq.type === 'arweave' && fileInfoReq.transactionId) {
fileObject = {
type: 'arweave',
transactionId: fileInfoReq.transactionId,
method: 'GET'
} as ArweaveFileObject
} else {
fileObject = { ...fileInfoReq } as StorageObject

fileInfoTask = {
command: PROTOCOL_COMMANDS.FILE_INFO,
file: fileObject,
type: fileObject.type as FileObjectType,
caller: req.caller
}
} else if (fileInfoReq.type === 'ftp' && fileInfoReq.url) {
fileObject = {
type: 'ftp',
url: fileInfoReq.url
} as FtpFileObject
fileInfoTask = {
command: PROTOCOL_COMMANDS.FILE_INFO,
file: fileObject,
type: FileObjectType.FTP,
caller: req.caller
}
}

const response = await new FileInfoHandler(req.oceanNode).handle(fileInfoTask)
if (response.stream) {
res.status(response.status.httpStatus)
Expand Down
4 changes: 2 additions & 2 deletions src/components/httpRoutes/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { DecryptDdoHandler } from '../core/handler/ddoHandler.js'
import { DownloadHandler } from '../core/handler/downloadHandler.js'
import { DownloadCommand } from '../../@types/commands.js'
import { FeesHandler } from '../core/handler/feesHandler.js'
import { BaseFileObject, EncryptMethod } from '../../@types/fileObject.js'
import { StorageObject, EncryptMethod } from '../../@types/fileObject.js'
import { P2PCommandResponse } from '../../@types/OceanNode.js'
import { getEncryptMethodFromString } from '../../utils/crypt.js'

Expand Down Expand Up @@ -119,7 +119,7 @@ providerRoutes.post(`${SERVICES_API_BASE_PATH}/encryptFile`, async (req, res) =>
if (req.is('application/json')) {
// body as fileObject
result = await new EncryptFileHandler(req.oceanNode).handle({
files: req.body as BaseFileObject,
files: req.body as StorageObject,
encryptionType: encryptMethod,
command: PROTOCOL_COMMANDS.ENCRYPT_FILE,
caller: req.caller,
Expand Down
Loading