Skip to content

Commit

Permalink
Merge pull request #46 from uploadcare/feature/group-from
Browse files Browse the repository at this point in the history
The high-level function for group uploading, aka groupFrom
  • Loading branch information
bautrukevich authored Jul 15, 2019
2 parents e41c52f + b046221 commit 13a07ca
Show file tree
Hide file tree
Showing 54 changed files with 1,704 additions and 280 deletions.
28 changes: 14 additions & 14 deletions .size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
{
"dist/uploadcare-upload-client.esm.js": {
"bundled": 49011,
"minified": 21736,
"gzipped": 5769,
"bundled": 59896,
"minified": 26701,
"gzipped": 6132,
"treeshaked": {
"rollup": {
"code": 16463,
"code": 21156,
"import_statements": 49
},
"webpack": {
"code": 19935
"code": 24345
}
}
},
"dist/uploadcare-upload-client.cjs.js": {
"bundled": 49099,
"minified": 21812,
"gzipped": 5794
"bundled": 60000,
"minified": 26793,
"gzipped": 6159
},
"dist/uploadcare-upload-client.js": {
"bundled": 97142,
"minified": 30810,
"gzipped": 9310
"bundled": 109205,
"minified": 35282,
"gzipped": 9740
},
"dist/uploadcare-upload-client.min.js": {
"bundled": 30994,
"minified": 30774,
"gzipped": 9311
"bundled": 35445,
"minified": 35235,
"gzipped": 9755
}
}
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ language: node_js

node_js:
- 'stable'
- 'lts/*'
- '8'

install:
- NODE_ENV=dev npm install
Expand Down
41 changes: 27 additions & 14 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,31 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## Added

* Wrappers for group paths of Upload API (`group`, `groupInfo`)
* `onUploadProgress` for Node.js
* Wrappers for group paths of Upload API (`group`, `groupInfo`).
* The high-level function for group uploading, aka filesGroupFrom.
* Uploading progress for Node.js in `base` method.

### Changed

* `UploadFromInterface` was renamed to `FileUploadInterface`.
* `FileProgress` was renamed to `ProgressParams`.
* `UploadcareFile` was renamed to `UploadcareFileInterface`.

[Unreleased]: https://github.com/uploadcare/uploadcare-upload-client/compare/v1.0.0-alpha.3...HEAD

## [1.0.0-alpha.3]

## Added

* Support `fileFrom` 'uploaded' file (`uuid`)
* Support `fileFrom` 'uploaded' file (`uuid`).
* Support of `waiting` status from `/from_url/status/` endpoint.
* Export some main types from `index.ts` file.
So you can import them now directly from `@uploadcare/upload-client`
* Throttling for `request`
* `retryThrottledMaxTimes` param to set count of max retries after throttled request (1 by default)
* `Uuid` type
* Mock server for local testing
* Export some main types from `index.ts` file.
So you can import them now directly from `@uploadcare/upload-client`.
* Throttling for `request`.
* `retryThrottledMaxTimes` param to set count of max retries after
throttled request (1 by default).
* `Uuid` type.
* Mock server for local testing.

## Fixed

Expand All @@ -39,23 +47,28 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Changed

* Project was moved from Flow notations to TypeScript.
* The `base` function now returns object that implements `DirectUploadInterface`.
* The `fileFrom` function now returns object that implements `UploadFromInterface`.
* The `base` function now returns object that implements
`DirectUploadInterface`.
* The `fileFrom` function now returns object that implements
`UploadFromInterface`.
* The `UCFile` type renamed to `UploadcareFile`.
* The progress of `fileFrom` now based on the `UploadingProgress` type.

### Added

* Low-level request wrappers for `/from_url/` and `/from_url/status/` paths of Upload API.
* Settings: the support of setting `baseCDN`, `checkForUrlDuplicates`, `saveUrlForRecurrentUploads`.
* Low-level request wrappers for `/from_url/` and `/from_url/status/`
paths of Upload API.
* Settings: the support of setting `baseCDN`, `checkForUrlDuplicates`,
`saveUrlForRecurrentUploads`.

[1.0.0-alpha.2]: https://github.com/uploadcare/uploadcare-upload-client/compare/v1.0.0-alpha.1...v1.0.0-alpha.2

## [1.0.0-alpha.1]

### Fixed

* Use the version from the `package.json` file to create Uploadcare User Agent.
* Use the version from the `package.json` file to create Uploadcare User
Agent.

### Changed

Expand Down
71 changes: 53 additions & 18 deletions mock-server/controllers/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,53 @@ import error from '../utils/error'
const UUID_REGEX = '[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}'
const GROUP_ID_REGEX = `${UUID_REGEX}~[1-9][0-9]*$`

/**
* Check file UUID.
* @param {string} uuid
* @return {boolean}
*/
const isValidUuid = (uuid: string): boolean => (new RegExp(UUID_REGEX)).test(uuid)

/**
* Check group id.
* @param {string} groupId
* @return {boolean}
*/
const isValidGroupId = (groupId: string): boolean => (new RegExp(GROUP_ID_REGEX)).test(groupId)

/**
* Get UUID from file
* @param {string} file
* @return {string}
*/
const getFileUuid = (file: string): string => {
// If file contains CDN operations
if ((new RegExp(/\//)).test(file)) {
const array = file.split('/')

return array[0]
}

return file
}

/**
* Is valid file?
* @param {string} file
* @return {boolean}
*/
const isValidFile = (file: string): boolean => {
const uuid = getFileUuid(file)

return isValidUuid(uuid)
}

/**
* '/group/'
* @param {object} ctx
*/
const index = (ctx) => {
const files = ctx.query && ctx.query['files[]']
let files = ctx.query && ctx.query['files[]']
const publicKey = ctx.query && ctx.query.pub_key

if (!files || files.length === 0) {
Expand All @@ -19,20 +60,16 @@ const index = (ctx) => {
})
}

if (files && files.length > 0) {
for (let key in files) {
if (files.hasOwnProperty(key)) {
const file = files[key]
const array = file.split('/')
const uuid = array[0]
const isValidUUID = (new RegExp(UUID_REGEX)).exec(uuid)

if (!isValidUUID) {
return error(ctx, {
statusText: `this is not valid file url: ${file}`
})
}
}
// If `files` contains only `string` – convert in array
if (!Array.isArray(files)) {
files = [files]
}

for (const file of files) {
if (!isValidFile(file)) {
return error(ctx, {
statusText: `this is not valid file url: ${file}`
})
}
}

Expand All @@ -58,9 +95,7 @@ const info = (ctx) => {
})
}

const isValidGroupId = (new RegExp(GROUP_ID_REGEX)).exec(groupId)

if (!isValidGroupId) {
if (!isValidGroupId(groupId)) {
return error(ctx, {
status: 404,
statusText: 'group_id is invalid.'
Expand Down
7 changes: 5 additions & 2 deletions mock-server/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import error from '../utils/error'
*/
const protectedRoutes: Array<string> = ROUTES
.filter((route: RouteType) => {
const path = Object.keys(route).shift()
const keys = Object.keys(route)
const path = keys[0]

return route[path].isProtected
})
.map((route: RouteType) => {
return Object.keys(route).shift()
const keys = Object.keys(route)

return keys[0]
})

/**
Expand Down
17 changes: 6 additions & 11 deletions mock-server/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// @ts-ignore
import Koa from 'koa'
import * as router from 'koa-route'
import chalk from 'chalk'

// Middleware
import cors from '@koa/cors'
import addTrailingSlashes from 'koa-add-trailing-slashes'
// @ts-ignore
import koaBody from 'koa-body'

import logger from './middleware/logger'
Expand All @@ -29,7 +31,8 @@ app.use(auth)

// Routes
ROUTES.forEach((route: RouteType) => {
const path = Object.keys(route).shift()
const keys = Object.keys(route)
const path = keys[0]
const method = route[path].method
const fn = route[path].fn

Expand All @@ -50,7 +53,8 @@ app.listen(PORT, () => {

// Print all available routes
ROUTES.forEach((route: RouteType) => {
const path = Object.keys(route).shift()
const keys = Object.keys(route)
const path = keys[0]
const routePath = route[path]
const method = routePath.method.toUpperCase()
const description = routePath.description || path
Expand All @@ -60,12 +64,3 @@ app.listen(PORT, () => {
})
console.log()
})
//
// app.use(() => {
//
// })
//
// app.use(async (ctx, next) => {
// process.stdout.write('\x1Bc')
// await next()
// })
5 changes: 2 additions & 3 deletions mock-server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"baseUrl": ".",
"resolveJsonModule": true
},
"exclude": [
"node_modules/**/*"
]
"include": ["src"],
"exclude": ["node_modules"]
}
Loading

0 comments on commit 13a07ca

Please sign in to comment.