Files
API

API

File uploading

createFileUpload()

Generate a signed URL using the createFileUpload() API. The API returns a signed URL, which is then used to upload the file directly from the user's browser to the object storage.

await roqBrowserClient.roqPlatform.createFileUpload({
    data: {
        name: file.name,
        contentType: file.type,
        fileCategory: 'USER_FILES',
    }
})
ParameterTypeDescription
namestringName of the uploaded file
contentTypestringMime type of the file, see here (opens in a new tab) for a list of all types
fileCategorystringKey of the category of the file

File reading

This pertains to accessing and retrieving data from the uploaded files.

files()

Get all files.

import { roqBrowserClient } from 'lib/roq/roq-client';
 
const allFiles = async () => {
	const allFiles = await roqBrowserClient.roqPlatform.files()
	return allFiles
}
ParameterTypeDescription
limitintegerThe number of files to return.
offsetintegerThe offset number of files.
orderobjectThe order of the files.
filter:entityNameobjectName of the object. This could be the name of the table in your database, e.g. "contract".
filter:entityReferencesobjectReferences (or IDs) of the related objects from your database.
filter:fileCategoryobjectFilter file by categories.
withFileCategorybooleanInclude file categories
withCreatedByUserbooleanInclude user data that created the file
withFileAssociationsbooleanInclude file associations

file()

Get file with specific file ID.

The file() API also provides information on the file category, its creator, and associations.

import { roqBrowserClient } from "lib/roq/roq-client"
 
const myfile = async () => {
	const myfile = await roqBrowserClient.roqPlatform.file({
		id: "e54b7785-f7c0-41cc-bfbe-06dbd1645ed9",
		withFileCategory: true,
		withCreatedByUser: true,
		withFileAssociations: true
	})
}
ParameterTypeDescription
idUUIDThe file ID
withFileCategorybooleanInclude file category
withCreatedByUserbooleanThe user that create the file
withFileAssociationsbooleanInclude file associations

File updating

updateFile()

You can use the updateFile() API to rename the uploaded files.

import { roqBrowserClient } from "lib/roq/roq-client"
 
const updateResult =async () => {
    return await roqBrowserClient.roqPlatform.updateFile({
        data: {
            fileId: "97eeb4da-d402-4a89-b034-bcb277b9e65c",
            name: "screenshot.png",
            status: FileStatusEnum.Ready
        }
    })
}  
ParameterTypeDescription
data:fileIdUUIDThe file ID to be updated.
data:namestringNew filename.
data:statusstringThe file status.

File deletion

deleteFiles()

To delete one or multiple files, use the deleteFiles() API.

import { roqBrowserClient } from "lib/roq/roq-client"
 
const delFiles =async () => {
    return await roqBrowserClient.asSuperAdmin().deleteFiles({
	    filter: {
		    id: {
			    equalTo: "76dada4e-4f07-456b-90e7-a43099f07052"
		    }
	    }
    })
}

To delete multiple files, utilize the valueIn filter option.

{
  filter: {
    id: {
      valueIn: ["file_id_1", "file_id_2"]
    }
  }
}
ParameterTypeDescription
filter:idobjectThe IDs of files to delete can be filtered using equalTo for a single file or valueIn for multiple file IDs.

File categories

File categories can be managed via API or ROQ Console.

Using the fileCategories() and fileCategory() APIs from the ROQ Platform, we can retrieve a list of all registered file categories and their details, respectively. These APIs can be easily used on the client-side via front-end SDK.

fileCategories()

Get all file categories.

import { roqBrowserClient } from "lib/roq/roq-client"
 
const doReadCategories = async () => {
	const allCategories = await roqBrowserClient.roqPlatform.fileCategories({})
	return allCategories
}
ParameterTypeDescription
limitintegerLimit the results
searchobjectSearch parameter
filterobjectFilter parameter
offsetintegerOffset the results
orderobjectOrder parameter

fileCategory()

Get file category details.

import { roqBrowserClient } from "lib/roq/roq-client"
 
const categoryDetails = async () => {
	const categoryDetail = await roqBrowserClient.roqPlatform.fileCategory({
		id: "ff8143f2-cd89-41e9-af16-999c9b9da9b8",
		withFileCategoryContentGroups: true
	})
	return categoryDetail
}
ParameterTypeDescription
idUUIDThe file category ID to be retrieved.
withFileCategoryContentGroupsbooleanInclude file category content groups.

File visibility

A file can be either public or private. Public means that there is a permanent URL that anyone can use, while private files can only be accessed by users who are allowed to do so. The visibility of a file can be changed in ROQ Console or API using the makeFilePrivate() and makeFilePublic(). These APIs also can be easily used on the client-side via front-end SDK.

makeFilePublic()

Enable public access to a file.

import { roqBrowserClient } from "lib/roq/roq-client"
 
const roqBrowserClient = useroqBrowserClient()
 
const filePublic =async () => {
    return await roqBrowserClient.roqPlatform.makeFilePublic({
        id: 'fileId'
    });
}
ParameterTypeDescription
idUUIDThe file ID to be made public.

makeFilePrivate()

Hide a file from public access.

import { roqBrowserClient } from "lib/roq/roq-client"
 
const roqBrowserClient = useroqBrowserClient()
 
cons private =async () => {
    return await roqBrowserClient.roqPlatform.makeFilePrivate({
        id: 'fileId'
    })
}
ParameterTypeDescription
idUUIDThe file ID to be made private.

File associations

ℹ️

This endpoint is only available from the server side of your application.

createFileAssociation()

Files usually belong to some other object. For instance, you may have PDFs which represent "contracts". Or you may have images which are "avatars" and so on. To simplify this, ROQ enables you to relate files with objects which are saved on your database. The advantage is that you don't need to add these relations to your own database. File associations will simplify the database schema.

await roqServerClient.roqPlatform.asSuperAdmin().createFileAssociation({
    data: {
        entityName: "purchase_history",
		entityReference: "3c0e2ce1-3105-447a-b214-ac1e0b1e7304",
		fileId: "97eeb4da-d402-4a89-b034-bcb277b9e65c"
  },
});
ParameterTypeDescription
fileIdUUIDThe ID of the file
entityReferenceUUIDReference (or ID) of the related entity in your database
entityNamestringName of the object. This could be the name of the table in your database, e.g. "purchase_history"

deleteFileAssociations()

Using this API will result in the deletion of any file associations.

const delFileAssoc = await roqServerClient.roqPlatform.asSuperAdmin().deleteFileAssociations({
    filter: {
	    id: {
		    equalTo: "7526d4b4-26db-4872-834d-68da2a1447e9"
	    }
    }
})
ParameterTypeDescription
filter:idobjectYou can use equalTo or valueIn for the ID of the file associations
filter:fileIdobjectYou can use equalTo or valueIn for the ID of the files