Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Media Manager class implementation #1

Merged
merged 24 commits into from Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8823727
Stub for file-based media manager query
MrOrz May 16, 2022
97f8554
Add integration test stub
MrOrz May 22, 2022
1bb0456
Fix GCS error "Unable to detect a Project Id in the current environme…
MrOrz May 22, 2022
6291d45
Implements file upload and get file info
MrOrz May 22, 2022
5dac8b9
Add insert integration
MrOrz May 22, 2022
0cd7cd0
Downgrade node-fetch so that it builds
MrOrz May 23, 2022
e83f9c4
Install node-fetch v2 type
MrOrz May 23, 2022
e5a8553
[MediaManager] fix GCS credential
MrOrz May 23, 2022
92c1942
Temp file name generation logic
MrOrz May 23, 2022
b2003ca
Fix prepareStream body stream access
MrOrz May 24, 2022
0723f18
Make integration test work now
MrOrz May 24, 2022
a2f8c0e
[MediaManager] rewrite insert() so that
MrOrz May 24, 2022
1ccbd72
Support node 16 and 18
MrOrz May 24, 2022
6e1c962
Apply secret to github workflow
MrOrz May 24, 2022
26795c1
Adjust onUploadStop so that it is invoked after temp file finished mo…
MrOrz May 24, 2022
90f36f0
Remove size check
MrOrz May 26, 2022
167f5b7
Extend timeout to 30 seconds
MrOrz May 26, 2022
0bc0cf1
Reduce test file from 1MiB to 100KiB
MrOrz May 26, 2022
b830113
Invoke onUploadStop on duplicate file as well
MrOrz May 26, 2022
194fb82
Move interface type definition outside of MediaManager
MrOrz May 26, 2022
e6a7f6c
Add integration test for .getContent(), .getInfo() and query()
MrOrz May 27, 2022
bbb3ab3
Avoid running 2 tests at the same time
MrOrz May 27, 2022
d9204b0
Includes content-type check in integration test
MrOrz Jun 10, 2022
404e950
Remove unnecessary test
MrOrz Jun 11, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions .env.sample
@@ -0,0 +1,12 @@
# Integration test.
#
# Note that all files in the specified bucket with prefix will
# be deleted on every integration test run.
#

# The whole JSON file content. Example:
# CREDENTIALS_JSON={"type": "service_account", "project_id": "...", ...}
CREDENTIALS_JSON=

BUCKET_NAME=
PREFIX=
6 changes: 5 additions & 1 deletion .github/workflows/main.yml
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
node: ['18.x']
node: ['16.x']
os: [ubuntu-latest]

steps:
Expand All @@ -27,6 +27,10 @@ jobs:

- name: Test
run: npm run test -- --ci --coverage --maxWorkers=2
env:
CREDENTIALS_JSON: ${{ secrets.CREDENTIALS_JSON }}
BUCKET_NAME: ${{ secrets.BUCKET_NAME }}
PREFIX: github/

- name: Build
run: npm run build
Expand Down
12 changes: 0 additions & 12 deletions .github/workflows/size.yml

This file was deleted.

3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -2,4 +2,5 @@
.DS_Store
node_modules
dist
coverage
coverage
.env
51 changes: 51 additions & 0 deletions README.md
Expand Up @@ -4,6 +4,44 @@

Design: https://g0v.hackmd.io/@mrorz/cofacts-media-manager

## Usage

```js
import MediaManager from '@cofacts/media-manager';

// Setup
const manager = new MediaManager({
credentialsJSON: process.env.GCS_CREDENTIALS,
bucketName: process.env.GCS_BUCKET_NAME,

// The prefix of where files are put.
// Trailing `/` is REQUIRED if you want files to be put in a "directory" on GCS web console. Otherwise, root directories will just have specified prefix in its front.
// If not given, files will be placed right insight the bucket.
prefix: 'some-dir/'
});

// Search
const { hits } = await manager.query({url: 'https://......'});

// Upload and index
const { id, url } = await manager.insert({url: 'https://......'});
```

### Install

Install cofacts/media-manager via npm:

```
npm i @cofacts/media-manager
```

### Service account permission
The service account should have the following [permissions](https://cloud.google.com/storage/docs/access-control/iam-roles) to the bucket:
- `storage.objects.list`
- `storage.objects.create`
- `storage.objects.get`
- `storage.objects.delete`

## Development

This project uses npm and Node.JS. Please use the Node.JS version specified in `engines` in `package.json`.
Expand Down Expand Up @@ -39,3 +77,16 @@ Use `npm run lint` to perform lint.
### Bundle Analysis

[`size-limit`](https://github.com/ai/size-limit) is set up to calculate the real cost of media-manager with `npm run size` and visualize the bundle with `npm run analyze`.

### Integration tests

Real integration tests are enabled if correct environment variables are provided.
Copy `.env.sample` to `.env` to get started.

If the environment variables are provided in `.env`, running `npm t` will also trigger an integration test, which will:

1. Cleanup files with `PREFIX` in `BUCKET` specified by `.env`
2. Start a static file server that serves files under `test/fixtures`
3. Instantiate `MediaManager` that connects to GCS using credentials provided in `.env`
4. Perform query & insertion on live Google Cloud Storage