Skip to content

Commit f6f6d2b

Browse files
committed
Added reading the data back from the File-Collection
1 parent 4aa1d7a commit f6f6d2b

File tree

8 files changed

+102
-30
lines changed

8 files changed

+102
-30
lines changed

.meteor/packages

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ blaze-html-templates # Compile .html files into Meteor Blaze views
1111
reactive-var # Reactive variable for tracker
1212
jquery # Helpful client-side library
1313
tracker # Meteor's client-side reactive programming library
14+
audit-argument-checks
15+
check
1416

1517
standard-minifier-css # CSS minifier run for production mode
1618
standard-minifier-js # JS minifier run for production mode
@@ -25,4 +27,6 @@ benjaminrh:jquery-cookie
2527
numeral:numeral
2628
twbs:bootstrap
2729
session
30+
aldeed:simple-schema
31+
mdg:validated-method
2832
vsivsi:file-collection

.meteor/versions

+4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ accounts-base@1.2.7
22
accounts-password@1.1.8
33
accounts-ui@1.1.9
44
accounts-ui-unstyled@1.1.12
5+
aldeed:simple-schema@1.5.3
56
allow-deny@1.0.4
7+
audit-argument-checks@1.0.7
68
autoupdate@1.2.9
79
babel-compiler@6.6.4
810
babel-runtime@0.1.8
@@ -43,6 +45,8 @@ less@2.6.0
4345
livedata@1.0.18
4446
localstorage@1.0.9
4547
logging@1.0.12
48+
mdg:validated-method@1.1.0
49+
mdg:validation-error@0.2.0
4650
meteor@1.1.14
4751
meteor-base@1.0.4
4852
minifier-css@1.1.11

client/colltest.html

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ <h3>file-collection JS Test App</h3>
44
<ul>
55
<li>Upload files by clicking <button type="button" class="btn btn-primary">Upload</button> or drag your files onto the grid.</li>
66
<li>Once uploaded you can download the files from the collection by clicking the link in the name column.</li>
7+
<li>Output the first 10K of data to the <b>File Data</b> panel below by pressing <button type="button" aria-hidden="true" class="btn btn-success btn-itsy">D</button>.</li>
78
<li>Delete files from the collection by clicking the <button type="button" aria-hidden="true" class="btn btn-danger btn-itsy"><span class="glyphicon glyphicon-remove"></span></button> button in the name column.<br>
89
Note: this deletion will remove all data of any aborted upload and cannot be restarted after deleting.</li>
910
</ul>
@@ -38,6 +39,7 @@ <h3>file-collection JS Test App</h3>
3839
<td>
3940
<button type="button" aria-hidden="true" class="btn btn-danger btn-itsy del-file"><span class="glyphicon glyphicon-remove"></span></button>
4041
{{#if length}}
42+
<button type="button" aria-hidden="true" class="btn btn-success btn-itsy dump-file">D</button>
4143
<a href="{{link}}?download=true">{{shortFilename 56}}</a>
4244
{{else}}
4345
{{shortFilename 56}}
@@ -81,5 +83,10 @@ <h3>file-collection JS Test App</h3>
8183
</tr>
8284
</tbody>
8385
</table>
86+
<br><br>
87+
<h4>File Data:</h4>
88+
<div class="panel panel-info">
89+
<div class="panel-body wrap">{{fileData}}</div>
90+
</div>
8491
</div>
8592
</template>

client/colltest.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Template.collTest.onCreated(function onCreated() {
99
'path': '/'
1010
});
1111
});
12+
template.autorun(function subFileData() {
13+
Meteor.subscribe('fileData');
14+
});
1215
});
1316

1417
Template.collTest.onRendered(function onRendered() {
@@ -30,7 +33,7 @@ function shorten(name, width) {
3033
retVal = name;
3134
}
3235
return retVal;
33-
};
36+
}
3437

3538
Template.collTest.events({
3639
'click .del-file': function cancelFile() {
@@ -47,9 +50,12 @@ Template.collTest.events({
4750
Uploads.resumable.pause();
4851
t.pause.set(true);
4952
},
50-
'click .fileResume': function pauseFiles(e, t) {
53+
'click .fileResume': function resumeFiles(e, t) {
5154
Uploads.resumable.upload();
5255
t.pause.set(false);
56+
},
57+
'click .dump-file': function dumpFile() {
58+
dumpData.call({'filename': this.filename});
5359
}
5460
});
5561

@@ -75,14 +81,16 @@ Template.collTest.helpers({
7581
}
7682
return cursor;
7783
},
84+
'fileData': function dataEntries() {
85+
let doc = fileData.findOne({});
86+
let data = doc && doc.data;
87+
return data;
88+
},
7889
'owner': function owner() {
7990
let ref = this.metadata;
8091
let FOwner = ref && ref._auth && ref._auth.owner;
8192
return FOwner;
8293
},
83-
// 'id': function id() {
84-
// return '' + this._id;
85-
// },
8694
'link': function getLink() {
8795
return Uploads.baseURL + '/md5/' + this.md5;
8896
},

lib/upload-collection.js renamed to lib/collections.js

+14
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,17 @@ Uploads = FileCollection({
1313
}
1414
]
1515
});
16+
17+
fileData = new Mongo.Collection('fileData');
18+
19+
fileData.allow({
20+
'insert': () => false,
21+
'update': () => false,
22+
'remove': () => true
23+
});
24+
25+
fileData.deny({
26+
'insert': () => true,
27+
'update': () => true,
28+
'remove': () => true
29+
});

lib/methods.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
dumpData = new ValidatedMethod({
2+
'name': 'dumpData',
3+
'validate': new SimpleSchema({
4+
'filename': { 'type': String }
5+
}).validator(),
6+
run({filename}) {
7+
if(!this.isSimulation) {
8+
let buffer = '';
9+
// because the stream is calling back in another context, need to bind
10+
// the callbacks to this context (e.g. so we can use the buffer variable)
11+
let bndStreamEnd = Meteor.bindEnvironment(function streamEnd() {
12+
fileData.upsert({'data': {'$exists': true}}, {'data': buffer});
13+
console.log(buffer);
14+
});
15+
let bndStreamData = Meteor.bindEnvironment(function streamData(buf) {
16+
// handle up to 10K
17+
if(buffer.length < 10000) {
18+
buffer = buffer + buf;
19+
}
20+
});
21+
// get the file stream
22+
let fStream = Uploads.findOneStream({'filename': filename});
23+
// handle the stream
24+
fStream.on('data', bndStreamData);
25+
fStream.on('end', bndStreamEnd);
26+
}
27+
}
28+
});

sample.css

+8
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@
2222
font-size: 6px;
2323
line-height: 7px;
2424
}
25+
26+
.wrap {
27+
white-space: pre-wrap;
28+
white-space: -moz-pre-wrap; /* Firefox */
29+
white-space: -pre-wrap; /* Opera <7 */
30+
white-space: -o-pre-wrap; /* Opera 7 */
31+
word-wrap: break-word; /* IE */
32+
}

server/sample.js

+24-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Meteor.startup(function serverStartup() {
22
Meteor.publish('allData', function allData(clientUserId) {
3+
check(clientUserId, null);
34
let retVal = [];
45
if(this.userId === clientUserId) {
56
retVal = Uploads.find({
@@ -12,32 +13,10 @@ Meteor.startup(function serverStartup() {
1213
return retVal;
1314
});
1415

15-
// Meteor.users.deny({
16-
// 'update': function update() {
17-
// return true;
18-
// }
19-
// });
20-
21-
Uploads.allow({
22-
'insert': function insert(userId, file) {
23-
file.metadata = file.metadata || {};
24-
file.metadata._auth = {'owner': userId};
25-
return true;
26-
},
27-
'remove': function remove(userId, file) {
28-
return file.metadata && file.metadata._auth &&
29-
file.metadata._auth.owner === userId;
30-
},
31-
'read': function read(userId, file) {
32-
return file.metadata && file.metadata._auth &&
33-
file.metadata._auth.owner === userId;
34-
},
35-
'write': function write(userId, file) {
36-
return file.metadata && file.metadata._auth &&
37-
file.metadata._auth.owner === userId;
38-
}
16+
Meteor.publish('fileData', function pubFileData() {
17+
return fileData.find({});
3918
});
40-
debugger;
19+
4120
let query = Uploads.find({'metadata._Resumable': {'$exists': false}});
4221
query.observe({
4322
removed: function cleanUpIncompleteFiles(rmFile) {
@@ -47,3 +26,23 @@ debugger;
4726
}
4827
});
4928
});
29+
30+
Uploads.allow({
31+
'insert': function insert(userId, file) {
32+
file.metadata = file.metadata || {};
33+
file.metadata._auth = {'owner': userId};
34+
return true;
35+
},
36+
'remove': function remove(userId, file) {
37+
return file.metadata && file.metadata._auth &&
38+
file.metadata._auth.owner === userId;
39+
},
40+
'read': function read(userId, file) {
41+
return file.metadata && file.metadata._auth &&
42+
file.metadata._auth.owner === userId;
43+
},
44+
'write': function write(userId, file) {
45+
return file.metadata && file.metadata._auth &&
46+
file.metadata._auth.owner === userId;
47+
}
48+
});

0 commit comments

Comments
 (0)