# media-hosting-miniprogram-sdk

> Tenncent Cloud Smart Media Hosting SDK for WeChat MiniProgram.

Latest version **0.2.0** (published 2020-12-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install media-hosting-miniprogram-sdk
pnpm add media-hosting-miniprogram-sdk
yarn add media-hosting-miniprogram-sdk
bun add media-hosting-miniprogram-sdk
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2020-12-16 |
| First published | 2020-12-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 171.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | fysntian |
| Maintainers | fysntian |

## Links

- npm: https://www.npmjs.com/package/media-hosting-miniprogram-sdk
- Repository: https://github.com/TencentCloud/media-hosting-miniprogram-sdk
- Homepage: https://github.com/TencentCloud/media-hosting-miniprogram-sdk#readme
- Issues: https://github.com/TencentCloud/media-hosting-miniprogram-sdk/issues
- npm.io page: https://npm.io/package/media-hosting-miniprogram-sdk

## Recent versions

- 0.2.0 (latest) — 2020-12-16
- 0.1.1 — 2020-12-14

## README

腾讯云智能媒资托管的小程序 SDK，使用 TypeScript 编写，自带类型定义并预构建为 JavaScript ES5。

## 安装

```bash
npm install media-hosting-miniprogram-sdk --save
```

## 接口文档

请参阅 [接口文档](https://smart-media-hosting-sdk-doc-1258344699.file.myqcloud.com/mp/globals.html)。

## 使用

- 导入 SDK
```typescript
import { MediaHosting } from 'media-hosting-miniprogram-sdk';
```

- 初始化实例
```typescript
const client = new MediaHosting({
    libraryId: 'smhxxx',
    getAccessToken(params, callback) {
        wx.request({
            // ...
        });
    },
});
```

- 列出相簿
```typescript
client.listDirectory((err, result) => {
    if (err) {
        if (err instanceof MediaHosting.RemoteError) {
            if (err.code === 'SpaceNotFound') {
                return wx.showModal({
                    content: '空间不存在',
                    confirmText: '确定',
                    showCancel: false,
                    success() {
                        wx.redirectTo({ url: '/' });
                    },
                });
            }
            // other Smart Media Hosting error
        } else if (err instanceof MediaHosting.WxRequestError) {
            // WeChat miniprogram error
        }
        // other unhandled error
        return wx.showToast({ icon: 'none', title: err.message, duration: 3000 });
    }
    const contents = result!.contents;
    // ...
});
```

- 上传文件
```typescript
wx.chooseMedia({
    maxDuration: 30,
    sizeType: ['original'],
    success(result) {
        for (const file of result.tempFiles) {
            client.uploadFile([
                albumName, // target album name
                file.tempFilePath.substr(file.tempFilePath.lastIndexOf('/') + 1),
                // get file name rather than full path
            ].join('/'), file.tempFilePath, (err) => {
                if (err) {
                    // upload failed
                    return;
                }
                // upload succeeded
            });
        }
    },
});
```

- 获取相簿封面 URL
```typescript
const albunNameList = contents.map(item => item.name); // string[]
client.getCoverUrl(albumNameList, size, (_err, result) => {
    // ignore error
    result = result || []; // if error occured, result is undefined
    const albums = albumNameList.map((name, index) => {
        return {
            name,
            coverUrl: result![index] || '',
            // note that we didn't handle any error so the coverUrl may be an empty string
        };
    });
    // use albums
});
```

- 全屏查看媒体
```typescript
const media = result!.contents; // listDirectory result
const pathList = media.map(item => [albumName, item.name].join('/')); // string[]
client.getFileUrl(pathList, (err, fileList) => {
    if (err) {
        // handle error
        return;
    }
    client.getPreviewUrl(pathList, (_err, previewList) => {
        // ignore error
        previewList = previewList || [];
        wx.previewMedia({
            sources: media.map((item, index) => {
                return {
                    url: fileList![index],
                    type: item.type,
                    poster: previewList![index] || '',
                };
            }),
            current: index, // current index
        });
    });
});
```

---
_Source: https://npm.io/package/media-hosting-miniprogram-sdk · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
