Skip to content

Commit

Permalink
add files
Browse files Browse the repository at this point in the history
  • Loading branch information
anarkrypto committed Mar 27, 2022
0 parents commit d8e7441
Show file tree
Hide file tree
Showing 21 changed files with 3,549 additions and 0 deletions.
104 changes: 104 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Sea.tube

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Sea Extractor

Simple micro-service REST API solution to extract data and metadata from videos such as thumbnails.

Ideally, we will soon have a more decentralized mechanism to carry out this process.

The service works with peertube links and soon with IPFS CIDs.

### Running:

Install dependencies:

```yarn```

Run as dev:

```yarn dev```

Endpoints:

#### /v1/thumbs
1 change: 1 addition & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NFT_STORAGE_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
6 changes: 6 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Allows friendly relative path and init api

require('module-alias/register')
import init from './src/init.js'

init()
67 changes: 67 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"name": "sea-extractor",
"version": "1.0.0",
"description": "Simple micro-service solution to extract data and metadata from videos such as thumbnails",
"main": "dist/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node dist/index.js",
"dev": "tsc -w & nodemon dist/index.js",
"build": "tsc"
},
"repository": {
"type": "git",
"url": "git+https://gitlab.com/sea-tubesea-extractor.git"
},
"keywords": [
"seatube",
"ipfs",
"livepeer",
"thumb",
"thumbnail",
"metadata",
"extract",
"api",
"rest"
],
"author": "anarkrypto",
"license": "MIT",
"bugs": {
"url": "https://gitlab.com/sea-tubesea-extractor/issues"
},
"homepage": "https://gitlab.com/sea-tubesea-extractor#readme",
"devDependencies": {
"@types/ajv": "^1.0.0",
"@types/axios": "^0.14.0",
"@types/cors": "^2.8.12",
"@types/dotenv": "^8.2.0",
"@types/express": "^4.17.13",
"@types/node": "^16.11.11",
"@types/validator": "^13.7.0",
"typescript": "^4.5.2"
},
"dependencies": {
"@types/mime": "^2.0.3",
"@types/uuid": "^8.3.4",
"ajv": "^8.8.2",
"ajv-formats": "^2.1.1",
"axios": "^0.24.0",
"cors": "^2.8.5",
"dotenv": "^16.0.0",
"express": "^4.17.1",
"module-alias": "^2.2.2",
"nft.storage": "^6.1.0",
"nodemon": "^2.0.15",
"uuid": "^8.3.2"
},
"_moduleAliases": {
"@root": "dist",
"@controllers": "dist/src/controllers",
"@models": "dist/src/models",
"@utils": "utils",
"@config": "dist/src/config"
},
"_moduleDirectories": [
"node_modules_custom"
]
}
6 changes: 6 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as dotenv from 'dotenv';
dotenv.config()

export const DEFAULT_DIR : string = process.env.DEFAULT_DIR || "/tmp/extractor-data"

export const NFT_STORAGE_API_KEY : string = process.env.NFT_STORAGE_API_KEY || ""
8 changes: 8 additions & 0 deletions src/controllers/api/notAllowed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ERROR_CODE } from "@controllers/utils/codes";

export default function notAllowed (req : any, res : any) {
res.status(ERROR_CODE.BAD_REQUEST).json({
status: "FAIL",
error: "invalid request"
})
}
103 changes: 103 additions & 0 deletions src/controllers/api/thumbs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import validator from '@models/validator'
import { ERROR_CODE } from '@controllers/utils/codes'
import ipFromReq from '@controllers/utils/ipFromReq';
import limitReached from '@controllers/utils/limits';
import { v4 as uuidv4 } from 'uuid';
import thumbVideo from '../../../tasks/ffmpeg';
import { mkdir } from 'fs';
import sheetMontage from '../../../tasks/montage';
import { DEFAULT_DIR } from "@config"
import { NFTStorage, File } from "nft.storage";
import { NFT_STORAGE_API_KEY } from "@config"
import path from 'path'
import fs from 'fs'

const client = new NFTStorage({ token: NFT_STORAGE_API_KEY || "" })

async function fileFromPath(filePath: string) {
const content = await fs.promises.readFile(filePath)
return new File([content], path.basename(filePath))
}


const storeFileIPFS = (imagePath: string) => {
return new Promise(async (resolve, reject) => {
try {
// const reader = new FileReader();
// reader.onload = async (e: any) => {
// const blob = new Blob([new Uint8Array(e.target.result)], { type: "image/png" });

const image = await fileFromPath(imagePath)

const cid = await client.storeBlob(image)

resolve(cid)

// };
// reader.readAsArrayBuffer(e.target.files[0]);
} catch (err) {
console.error(err)
reject(err)
}
})
}

export default async function thumbnails(req: any, res: any) {

// Check IP limits
const ip = ipFromReq(req)
if (limitReached("thumb", ip, 5)) {
res.status(ERROR_CODE.FORBIDDEN).json(
{
status: "FAIL",
error: "IP limit reached"
}
)
}

// Try Parse Request JSON
let json: any = {}
try {
json = JSON.parse(req.body)
} catch (e) {
return res.status(ERROR_CODE.BAD_REQUEST).json({
error: "Unable to parse JSON"
});
}

// Validate Request Scheme
const valid = validator.thumbs(json)
if (valid !== true) return res.status(ERROR_CODE.BAD_REQUEST).json({ status: "fail", errors: valid.errors })

const id = uuidv4()

try {

await mkdir(`${DEFAULT_DIR}/${id}`, (err) => {
if (err) {
throw (err);
}
console.log(`Directory ${id} created successfully!`);
});

await thumbVideo(id, json.url, .2, true) // 1 thumbs per each 5 second

await sheetMontage(id, `${DEFAULT_DIR}/${id}/`, `${DEFAULT_DIR}/`)

const cid = await storeFileIPFS(`${DEFAULT_DIR}/${id}.png`)

console.log(cid)

res.status(200).json(
{
status: "CREATED",
image: "example.png"
})
} catch (err) {
res.status(ERROR_CODE.INTERNAL_SERVER_ERROR).json(
{
status: "FAIL",
error: err
})
}
}
13 changes: 13 additions & 0 deletions src/controllers/utils/codes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// HTTP Error Codes
const ERROR_CODE = {
BAD_REQUEST: 400,
UNAUTHORIZED: 401,
FORBIDDEN: 403,
NOT_FOUND: 404,
INTERNAL_SERVER_ERROR: 500,
BAD_GATEWAY: 502,
SERVICE_UNAVAILABLE: 503,
GATEWAY_TIMEOUT: 504
}

export { ERROR_CODE }
20 changes: 20 additions & 0 deletions src/controllers/utils/ipFromReq.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
The mechanism used to get the real IP from the client may vary depending on your server and your needs.
Getting just req.ip can for example return your server's network address instead of the client/user address.
Be sure to choose the best method and adjust the function below according to your needs.
Read more: https://stackoverflow.com/questions/10849687/express-js-how-to-get-remote-client-address
*/

import { Request } from "express"

// This is an example when the request has to go through multiple proxies.
// X-Forwarded-For: 103.0.113.165, 60.91.3.17, 120.192.338.678
export function removeProxy(req_ip: string | string[]) : string {
const ip_list = req_ip.toString().split(", ")
return ip_list[ip_list.length - 1]
}

// The following method works with Heroku
export default function ipFromReq(req: Request): string {
return removeProxy(req.headers['x-forwarded-for'] || req.socket.remoteAddress || req.ip)
}
Loading

0 comments on commit d8e7441

Please sign in to comment.