Skip to content

Commit 3b4dd5e

Browse files
authored
Merge pull request #744 from VeliovGroup/dev
v1.14.2 - 🐞 Fix #742, thanks to @menelike and @jankapunkt - 🤓 Update *TypeScript* definitions, thanks to @OliverColeman PR #743, see #226 thread for details - 🤝 Compatibility with `[email protected]` - 📋 Documentation update to address issue described in #737, thanks to @menelike, @Lickshotz, @s-ol, and @jankapunkt
2 parents 0f73a36 + 9db7452 commit 3b4dd5e

File tree

9 files changed

+1592
-403
lines changed

9 files changed

+1592
-403
lines changed

.npm/package/npm-shrinkwrap.json

Lines changed: 1099 additions & 239 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.versions

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ [email protected]
2222
2323
2424
25-
local-test:ostrio:[email protected].1
25+
local-test:ostrio:[email protected].2
2626
2727
2828
@@ -36,7 +36,7 @@ [email protected]
3636
3737
3838
39-
39+
4040
4141
4242

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
- [Why this package?](https://github.com/VeliovGroup/Meteor-Files#why-meteor-files)
2828
- [Installation](https://github.com/VeliovGroup/Meteor-Files#installation)
2929
- [ES6 Import](https://github.com/VeliovGroup/Meteor-Files#es6-import)
30-
- [TypeScript Definitions](https://github.com/VeliovGroup/Meteor-Files/wiki/TypeScript-definitions)
30+
- [TypeScript Definitions](https://github.com/VeliovGroup/Meteor-Files/blob/master/docs/typescript-definitions.md)
3131
- [FAQ](https://github.com/VeliovGroup/Meteor-Files#faq)
3232
- [API](https://github.com/VeliovGroup/Meteor-Files#api-overview-full-api):
3333
- [Initialize Collection](https://github.com/VeliovGroup/Meteor-Files#new-filescollectionconfig-isomorphic)
@@ -109,6 +109,8 @@ import { FilesCollection } from 'meteor/ostrio:files';
109109
4. When using any of `accounts` packages - package `accounts-base` must be explicitly added to `.meteor/packages` above `ostrio:files`
110110
5. __cURL/POST uploads__ - Take a look on [POST-Example](https://github.com/noris666/Meteor-Files-POST-Example) by [@noris666](https://github.com/noris666)
111111
6. In __Safari__ (Mobile and Desktop) for `DDP` upload streams are hard-coded to `1` and chunk size is reduced by algorithm, due to error thrown if too many connection is open by the browser or frame is too big. Limit simultaneous uploads to `6` is recommended for Safari. This issue should be fixed in Safari 11. Switching to `http` transport (*which has no such issue*) is recommended for Safari. See [#458](https://github.com/VeliovGroup/Meteor-Files/issues/458)
112+
7. Make sure you're using single domain for the Meteor app, and the same domain for hosting Meteor-Files endpoints, see [#737](https://github.com/VeliovGroup/Meteor-Files/issues/737) for details
113+
8. When proxying requests to Meteor-Files endpoint make sure protocol `http/1.1` is used, see [#742](https://github.com/VeliovGroup/Meteor-Files/issues/742) for details
112114

113115
## API overview (*[full API](https://github.com/VeliovGroup/Meteor-Files/wiki)*)
114116

docs/custom-response-headers.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,67 @@
1-
Please refer to [`docs/custom-response-headers.md`](https://github.com/VeliovGroup/Meteor-Files/blob/master/docs/custom-response-headers.md).
1+
# Custom Response Headers
2+
3+
- `config.responseHeaders` option (*passed into [`FilesCollection` Constructor](https://github.com/VeliovGroup/Meteor-Files/wiki/Constructor)*)
4+
5+
*Allows to change default response headers.*
6+
7+
## Default function:
8+
9+
We recommend to keep original function structure, with your modifications
10+
11+
```js
12+
function responseHeaders (responseCode, fileRef, versionRef) {
13+
const headers = {};
14+
switch (responseCode) {
15+
case '206':
16+
headers['Pragma'] = 'private';
17+
headers['Transfer-Encoding'] = 'chunked';
18+
break;
19+
case '400':
20+
headers['Cache-Control'] = 'no-cache';
21+
break;
22+
case '416':
23+
headers['Content-Range'] = 'bytes */' + versionRef.size;
24+
}
25+
headers['Connection'] = 'keep-alive';
26+
headers['Content-Type'] = versionRef.type || 'application/octet-stream';
27+
headers['Accept-Ranges'] = 'bytes';
28+
return headers;
29+
}
30+
```
31+
32+
## Adding custom header example:
33+
34+
We recommend to pass `responseHeaders` as a <em>Function</em>, response headers __should be conditional__.
35+
36+
```js
37+
// As function (keep original function with additions):
38+
const Uploads = new FilesCollection({
39+
responseHeaders(responseCode, fileRef, versionRef, version, http) {
40+
const headers = {};
41+
switch (responseCode) {
42+
case '206':
43+
headers['Pragma'] = 'private';
44+
headers['Transfer-Encoding'] = 'chunked';
45+
break;
46+
case '400':
47+
headers['Cache-Control'] = 'no-cache';
48+
break;
49+
case '416':
50+
headers['Content-Range'] = 'bytes */' + versionRef.size;
51+
}
52+
headers['Connection'] = 'keep-alive';
53+
headers['Content-Type'] = versionRef.type || 'application/octet-stream';
54+
headers['Accept-Ranges'] = 'bytes';
55+
headers['Access-Control-Allow-Origin'] = '*';// <-- Custom header
56+
return headers;
57+
}
58+
});
59+
60+
// As object (not recommended):
61+
const Uploads = new FilesCollection({
62+
responseHeaders: {
63+
Connection: 'keep-alive',
64+
'Access-Control-Allow-Origin': '*'
65+
}
66+
});
67+
```

docs/toc.md

Lines changed: 92 additions & 92 deletions
Large diffs are not rendered by default.

docs/typescript-definitions.md

Lines changed: 102 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@
22

33
```ts
44
declare module "meteor/ostrio:files" {
5-
65
import { Mongo } from 'meteor/mongo';
76
import { ReactiveVar } from 'meteor/reactive-var';
7+
import { SimpleSchemaDefinition } from 'simpl-schema';
8+
9+
10+
interface Version<MetadataType> {
11+
extension: string;
12+
meta: MetadataType;
13+
path: string;
14+
size: number;
15+
type: string;
16+
}
17+
818

9-
class FileObj {
19+
class FileObj<MetadataType> {
20+
_id: string;
1021
size: number;
1122
name: string;
1223
type: string;
@@ -17,92 +28,112 @@ declare module "meteor/ostrio:files" {
1728
isText: boolean;
1829
isJSON: boolean;
1930
isPDF: boolean;
31+
ext?: string;
2032
extension?: string;
33+
extensionWithDot: string;
2134
_storagePath: string;
2235
_downloadRoute: string;
2336
_collectionName: string;
2437
public?: boolean;
25-
meta?: Object;
38+
meta?: MetadataType;
2639
userId?: string;
2740
updatedAt?: Date;
28-
versions: Object;
41+
versions: {
42+
[propName: string]: Version<MetadataType>;
43+
};
44+
mime: string;
45+
"mime-type": string;
2946
}
3047

31-
type FileRef = any; // File record from Mongo DB... don't know the details yet
3248

33-
interface FileData {
49+
type FileRef<MetadataType> = FileObj<MetadataType> & {
50+
remove: (callback: (error: any) => void) => void;
51+
link: (version?: string, location?: string) => string;
52+
get: (property?: string) => FileObj<MetadataType> | any;
53+
fetch: () => FileObj<MetadataType>[]
54+
with: () => FileCursor<MetadataType>
55+
}
56+
57+
58+
interface FileData<MetadataType> {
3459
size: number;
3560
type: string;
3661
mime: string;
3762
"mime-type": string;
3863
ext: string;
3964
extension: string;
4065
name: string;
66+
meta: MetadataType;
4167
}
4268

43-
interface FilesCollectionConfig {
44-
storagePath?: string;
45-
collection?: Mongo.Collection<any>;
69+
70+
interface FilesCollectionConfig<MetadataType> {
71+
storagePath?: string | ((fileObj: FileObj<MetadataType>) => string);
72+
collection?: Mongo.Collection<FileObj<MetadataType>>;
4673
collectionName?: string;
4774
continueUploadTTL?: string;
4875
ddp?: Object;
4976
cacheControl?: string;
50-
responseHeaders?: { [x: string]: string } | ((responseCode?, fileRef?, versionRef?, version?) => { [x: string]: string });
77+
responseHeaders?: { [x: string]: string } | ((responseCode?: string, fileRef?: FileRef<MetadataType>, versionRef?: Version<MetadataType>, version?: string) => { [x: string]: string });
5178
throttle?: number | boolean;
5279
downloadRoute?: string;
53-
schema?: Object;
80+
schema?: SimpleSchemaDefinition;
5481
chunkSize?: number;
55-
namingFunction?: () => string;
82+
namingFunction?: (fileObj: FileObj<MetadataType>) => string;
5683
permissions?: number;
5784
parentDirPermissions?: number;
5885
integrityCheck?: boolean;
5986
strict?: boolean;
60-
downloadCallback?: (fileObj: FileObj) => boolean;
61-
protected?: boolean | ((fileObj: FileObj) => boolean | number);
87+
downloadCallback?: (fileObj: FileObj<MetadataType>) => boolean;
88+
protected?: boolean | ((fileObj: FileObj<MetadataType>) => boolean | number);
6289
public?: boolean;
63-
onBeforeUpload?: (fileData: FileData) => boolean | string;
64-
onBeforeRemove?: (cursor: Mongo.Cursor<any>) => boolean;
65-
onInitiateUpload?: (fileData: FileData) => void;
66-
onAfterUpload?: (fileRef: FileRef) => any;
67-
onAfterRemove?: (files: Object[]) => any;
90+
onBeforeUpload?: (fileData: FileData<MetadataType>) => boolean | string;
91+
onBeforeRemove?: (cursor: Mongo.Cursor<FileObj<MetadataType>>) => boolean;
92+
onInitiateUpload?: (fileData: FileData<MetadataType>) => void;
93+
onAfterUpload?: (fileRef: FileRef<MetadataType>) => any;
94+
onAfterRemove?: (files: FileObj<MetadataType>[]) => any;
6895
onbeforeunloadMessage?: string | (() => string);
6996
allowClientCode?: boolean;
7097
debug?: boolean;
71-
interceptDownload?: (http: any, fileRef: any, version: string) => boolean;
98+
interceptDownload?: (http: Object, fileRef: FileRef<MetadataType>, version: string) => boolean;
7299
}
100+
73101

74-
export interface SearchOptions {
102+
export interface SearchOptions<MetadataType, TransformedType> {
75103
sort?: Mongo.SortSpecifier;
76104
skip?: number;
77105
limit?: number;
78106
fields?: Mongo.FieldSpecifier;
79107
reactive?: boolean;
80-
transform?: Function;
108+
transform?: (fileObj: FileObj<MetadataType>) => FileObj<TransformedType>;
81109
}
110+
82111

83-
export interface InsertOptions {
112+
export interface InsertOptions<MetadataType> {
84113
file: File | Object | string;
85114
isBase64?: boolean;
86-
meta?: { [x: string]: any };
115+
meta?: MetadataType;
87116
transport?: 'ddp' | 'http'
88-
onStart?: (error: Object, fileData: Object) => any;
89-
onUploaded?: (error: Object, fileData: Object) => any;
90-
onAbort?: (fileData: Object) => any;
91-
onError?: (error: Object, fileData: Object) => any;
92-
onProgress?: (progress: number, fileData: Object) => any;
93-
onBeforeUpload?: (fileData: Object) => any;
117+
onStart?: (error: Object, fileData: FileData<MetadataType>) => any;
118+
onUploaded?: (error: Object, fileRef: FileRef<MetadataType>) => any;
119+
onAbort?: (fileData: FileData<MetadataType>) => any;
120+
onError?: (error: Object, fileData: FileData<MetadataType>) => any;
121+
onProgress?: (progress: number, fileData: FileData<MetadataType>) => any;
122+
onBeforeUpload?: (fileData: FileData<MetadataType>) => any;
94123
streams?: number | 'dynamic';
95124
chunkSize?: number | 'dynamic';
96125
allowWebWorkers?: boolean;
97126
}
98127

99-
export interface LoadOptions {
128+
129+
export interface LoadOptions<MetadataType> {
100130
fileName: string;
101-
meta?: Object;
131+
meta?: MetadataType;
102132
type?: string;
103133
size?: number;
104134
}
105135

136+
106137
export class FileUpload {
107138
file: File;
108139
onPause: ReactiveVar<boolean>;
@@ -119,16 +150,18 @@ declare module "meteor/ostrio:files" {
119150
on(event: string, callback: Function): void;
120151
}
121152

122-
export class FileCursor extends FileObj { // Is it correct to say that it extends FileObj?
153+
154+
export class FileCursor<MetadataType> extends FileObj<MetadataType> { // Is it correct to say that it extends FileObj?
123155
remove(callback: (err) => void): void;
124156
link(): string;
125157
get(property: string): Object | any;
126158
fetch(): Object[];
127-
with(): ReactiveVar<FileCursor>;
159+
with(): ReactiveVar<FileCursor<MetadataType>>;
128160
}
129161

130-
export class FilesCursor extends Mongo.Cursor<FileObj> {
131-
cursor: Mongo.Cursor<FileObj>; // Refers to base cursor? Why is this existing?
162+
163+
export class FilesCursor<MetadataType> extends Mongo.Cursor<FileObj<MetadataType>> {
164+
cursor: Mongo.Cursor<FileObj<MetadataType>>; // Refers to base cursor? Why is this existing?
132165

133166
get(): Object[];
134167
hasNext(): boolean;
@@ -138,29 +171,46 @@ declare module "meteor/ostrio:files" {
138171
first(): Object;
139172
last(): Object;
140173
remove(callback: (err) => void): void;
141-
each(): FileCursor[];
174+
each(): FileCursor<MetadataType>[];
142175
current(): Object | undefined;
143176
}
144177

145-
export class FilesCollection {
146-
collection: Mongo.Collection<FileObj>;
147-
schema: any;
148-
149-
constructor(config: FilesCollectionConfig)
150178

151-
find(selector?: Mongo.Selector, options?: SearchOptions): FilesCursor;
152-
findOne(selector?: Mongo.Selector, options?: SearchOptions): FileCursor;
153-
insert(settings: InsertOptions, autoStart?: boolean): FileUpload;
154-
remove(select: Mongo.Selector, callback: (error) => any): FilesCollection;
155-
link(fileRef: FileRef, version?: string): string;
179+
export class FilesCollection<MetadataType = { [x: string]: any }> {
180+
collection: Mongo.Collection<FileObj<MetadataType>>;
181+
schema: SimpleSchemaDefinition;
182+
183+
constructor(config: FilesCollectionConfig<MetadataType>)
184+
185+
/**
186+
* Find and return Cursor for matching documents.
187+
*
188+
* @param selector [[http://docs.meteor.com/api/collections.html#selectors | Mongo-Style selector]]
189+
* @param options [[http://docs.meteor.com/api/collections.html#sortspecifiers | Mongo-Style selector Options]]
190+
191+
* @typeParam TransformedType The result of transforming a document with options.tranform().
192+
*/
193+
find<TransformedType = FileRef<MetadataType>>(selector?: Mongo.Selector<Partial<FileObj<MetadataType>>>, options?: SearchOptions<MetadataType, TransformedType>): FilesCursor<TransformedType>;
194+
/**
195+
* Finds the first document that matches the selector, as ordered by sort and skip options.
196+
*
197+
* @param selector [[http://docs.meteor.com/api/collections.html#selectors | Mongo-Style selector]]
198+
* @param options [[http://docs.meteor.com/api/collections.html#sortspecifiers | Mongo-Style selector Options]]
199+
200+
* @typeParam TransformedType The result of transforming a document with options.tranform().
201+
*/
202+
findOne<TransformedType = FileRef<MetadataType>>(selector?: Mongo.Selector<Partial<FileObj<MetadataType>>> | string, options?: SearchOptions<MetadataType, TransformedType>): FileCursor<TransformedType>;
203+
insert(settings: InsertOptions<MetadataType>, autoStart?: boolean): FileUpload;
204+
remove(select: Mongo.Selector<FileObj<MetadataType>> | string, callback?: (error: Object) => Object): FilesCollection<MetadataType>;
205+
link(fileRef: FileRef<MetadataType>, version?: string): string;
156206
allow(options: Mongo.AllowDenyOptions): void;
157207
deny(options: Mongo.AllowDenyOptions): void;
158208
denyClient(): void;
159-
on(event: string, callback: (fileRef: FileRef) => void): void;
160-
unlink(fileRef: FileRef, version?: string): FilesCollection;
161-
addFile(path: string, opts: LoadOptions, callback: (err: any, fileRef: FileRef) => any, proceedAfterUpload: boolean);
162-
load(url: string, opts: LoadOptions, callback: (err: any, fileRef: FileRef) => any, proceedAfterUpload: boolean);
163-
write(buffer: Buffer, opts: LoadOptions, callback: (err: any, fileRef: FileRef) => any, proceedAfterUpload: boolean);
209+
on(event: string, callback: (fileRef: FileRef<MetadataType>) => void): void;
210+
unlink(fileRef: FileRef<MetadataType>, version?: string): FilesCollection<MetadataType>;
211+
addFile(path: string, opts: LoadOptions<MetadataType>, callback: (err: any, fileRef: FileRef<MetadataType>) => any, proceedAfterUpload: boolean): FilesCollection<MetadataType>;
212+
load(url: string, opts: LoadOptions<MetadataType>, callback: (err: Object, fileRef: FileRef<MetadataType>) => any, proceedAfterUpload: boolean): FilesCollection<MetadataType>;
213+
write(buffer: Buffer, opts: LoadOptions<MetadataType>, callback: (err: Object, fileRef: FileRef<MetadataType>) => any, proceedAfterUpload: boolean): FilesCollection<MetadataType>;
164214
}
165215
}
166216
```

0 commit comments

Comments
 (0)