-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Mikael Finstad <finstaden@gmail.com>
- Loading branch information
Showing
6 changed files
with
329 additions
and
169 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { Body, Meta, UppyFile } from '@uppy/utils/lib/UppyFile' | ||
import * as tus from 'tus-js-client' | ||
|
||
function isCordova() { | ||
return ( | ||
typeof window !== 'undefined' && | ||
// @ts-expect-error may exist | ||
(typeof window.PhoneGap !== 'undefined' || | ||
// @ts-expect-error may exist | ||
typeof window.Cordova !== 'undefined' || | ||
// @ts-expect-error may exist | ||
typeof window.cordova !== 'undefined') | ||
) | ||
} | ||
|
||
function isReactNative() { | ||
return ( | ||
typeof navigator !== 'undefined' && | ||
typeof navigator.product === 'string' && | ||
navigator.product.toLowerCase() === 'reactnative' | ||
) | ||
} | ||
|
||
// We override tus fingerprint to uppy’s `file.id`, since the `file.id` | ||
// now also includes `relativePath` for files added from folders. | ||
// This means you can add 2 identical files, if one is in folder a, | ||
// the other in folder b — `a/file.jpg` and `b/file.jpg`, when added | ||
// together with a folder, will be treated as 2 separate files. | ||
// | ||
// For React Native and Cordova, we let tus-js-client’s default | ||
// fingerprint handling take charge. | ||
export default function getFingerprint<M extends Meta, B extends Body>( | ||
uppyFile: UppyFile<M, B>, | ||
): tus.UploadOptions['fingerprint'] { | ||
return (file, options) => { | ||
if (isCordova() || isReactNative()) { | ||
return tus.defaultOptions.fingerprint!(file, options) | ||
} | ||
|
||
const uppyFingerprint = ['tus', uppyFile.id, options!.endpoint].join('-') | ||
|
||
return Promise.resolve(uppyFingerprint) | ||
} | ||
} |
17 changes: 13 additions & 4 deletions
17
packages/@uppy/tus/src/index.test.js → packages/@uppy/tus/src/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,38 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import Core from '@uppy/core' | ||
import Tus from './index.js' | ||
import Tus from './index.ts' | ||
|
||
describe('Tus', () => { | ||
it('Throws errors if autoRetry option is true', () => { | ||
const uppy = new Core() | ||
|
||
expect(() => { | ||
// @ts-expect-error removed | ||
uppy.use(Tus, { autoRetry: true }) | ||
}).toThrowError(/The `autoRetry` option was deprecated and has been removed/) | ||
}).toThrowError( | ||
/The `autoRetry` option was deprecated and has been removed/, | ||
) | ||
}) | ||
|
||
it('Throws errors if autoRetry option is false', () => { | ||
const uppy = new Core() | ||
|
||
expect(() => { | ||
// @ts-expect-error removed | ||
uppy.use(Tus, { autoRetry: false }) | ||
}).toThrowError(/The `autoRetry` option was deprecated and has been removed/) | ||
}).toThrowError( | ||
/The `autoRetry` option was deprecated and has been removed/, | ||
) | ||
}) | ||
|
||
it('Throws errors if autoRetry option is `undefined`', () => { | ||
const uppy = new Core() | ||
|
||
expect(() => { | ||
// @ts-expect-error removed | ||
uppy.use(Tus, { autoRetry: undefined }) | ||
}).toThrowError(/The `autoRetry` option was deprecated and has been removed/) | ||
}).toThrowError( | ||
/The `autoRetry` option was deprecated and has been removed/, | ||
) | ||
}) | ||
}) |
Oops, something went wrong.