# Media

Our Media module stands out as a potent tool for efficient and secure management and sharing of your image and video content. This module streamlines the process of uploading multimedia files to cloud storage, offering a hassle-free experience.

Primary Benefit

* Seamlessly integrates with your workflow through its user-friendly API, reducing the need for manual database management.
* Eliminates the complexities of setting up databases and managing AWS servers, allowing you to focus on your core tasks

{% hint style="info" %}
**Note**: There is no file size limitation for uploads via the SDK
{% endhint %}

### Adding Module <a href="#adding-module" id="adding-module"></a>

{% tabs %}
{% tab title="Capacitor" %}
{% code overflow="wrap" %}

```json
npm install @trunpm/truvideo-capacitor-media-sdk --registry=https://registry.npmjs.org/ --//registry.npmjs.org/:_authToken=<YOUR_TOKEN>
```

{% endcode %}
{% endtab %}
{% endtabs %}

Import the following dependency in your file, here you will use these function.

{% tabs %}
{% tab title="TSX" %}
{% code overflow="wrap" %}

```typescript
import {  
    MediaBuilder, 
    type UploadCompleteEventData, 
    type UploadErrorEvent, 
    type UploadProgressEvent 
} from '@trunpm/truvideo-capacitor-media-sdk';
```

{% endcode %}
{% endtab %}
{% endtabs %}

### Create Upload Callback -

The uploadMedia function emits several events during the upload process. These events can be listened to using the NativeEventEmitter from React Native.

* onProgress: Emitted to indicate the progress of the upload.
* onError: Emitted if there is an error during the upload.
* onComplete: Emitted when the upload is complete.

{% tabs %}
{% tab title="JSX" %}

```jsx
// Upload
const uploadCallbacks = {
  onProgress: (event: UploadProgressEvent) => {
    console.log(`[uploadMediaItems] Upload Progress | ID: ${event.id}, Progress: ${event.progress}%`);
  },
  onComplete: (event: UploadCompleteEventData) => {
    console.log(`[uploadMediaItems] Upload Complete | ID: ${event.id}, Type: ${event.fileType}`);
  },
  onError: (event: UploadErrorEvent) => {
    console.error(`[uploadMediaItems] Upload Error | ID: ${event.id}, Error: ${event.error}`);
  },
}
```

{% endtab %}
{% endtabs %}

### Returned Result With Upload

```tsx
interface UploadCompleteEventData {
  id: string;
  createdDate?: string;
  remoteId?: string;
  uploadedFileURL?: string;
  metaData?: string; 
  tags?: string;
  transcriptionURL?: string;
  transcriptionLength?: number;
  fileType?: string;
}
```

### Upload Media

`TruVideoMediaSdk` contains all function required to manage the media module. UploadFile function upload’s a one file at a time i.e. photo or video by taking, URI of the local file path, tag, metaData and it will return promise. Promise that resolves with the response from the server upon successful upload, or rejects with an error if the upload fails.

{% tabs %}
{% tab title="JSX" %}

```tsx
const result = new MediaBuilder(item.filePath);
console.log(`[uploadMediaItems] Initialized MediaBuilder for ${item.filePath}`);

// Set tags
result.setTag("key", "value");
result.setTag("color", "red");
result.setTag("orderNumber", "123");

// Set metadata
result.setMetaData("key", "value");
result.setMetaData("key1", "1");
result.setMetaData("key2", "[4,5,6]");

// Build request
const request = await result.build();
console.log("[uploadMediaItems] Built upload request");

// uploadCallbacks obbject from previous step 
await request.upload(uploadCallbacks);
console.log(`[uploadMediaItems] Upload finished for: ${item.filePath}`);
```

{% endtab %}
{% endtabs %}

### Search Media&#x20;

search function will give the list uploaded media with respect to tags, page, pageSize and type

```tsx
const searchData : UploadCompleteEventData[] = 
    search(
        tags: Map<string, string>,
        page: number,
        pageSize: number,
        type?: MediaType
    );
    
// media type 
enum MediaType {
  IMAGE,
  VIDEO,
  AUDIO,
  PDF,
}
```

### GetAllRequest

This function will provide all the requests created for Upload Builder

* Input Params&#x20;

```tsx
var status : UploadRequestStatus | undefined = undefined // for all status 

// for specific status 
enum UploadRequestStatus {
  UPLOADING,
  IDLE,
  ERROR,
  PAUSED,
  COMPLETED,
  CANCELED,
  SYNCHRONIZING,
}
```

* Function Utilisation&#x20;

```tsx
const requests : MediaData[]  = await getAllFileUploadRequests(status);
```

### GetRequestById

This function will return exact request associated with the id&#x20;

```tsx
const request : MediaData = getFileUploadRequestById(id : string);
```

### MediaData

```tsx
export interface MediaData {
    id: string;
    filePath: string;
    fileType: string;
    durationMilliseconds: number;
    remoteId: string;
    remoteURL: string;
    transcriptionURL: string;
    transcriptionLength: number;
    status: string;
    progress: number;
}
```
