手を付けられず・・・orz
・CollectionFS
・filesystem(アプリケーションサーバー)
・mongodb(gridfs)
・クラウドストレージ(AWS-s3, dropbox)
・CollectionFS(ファイルマネージャー)
$ meteor add cfs:standard-packages
$ meteor add cfs:gridfs
var imageStore = new FS.Store.GridFS("images", {
mongoUrl: 'mongodb://127.0.0.1:27017/test/', // optional, defaults to Meteor's local MongoDB
mongoOptions: {...}, // optional, see note below
transformWrite: myTransformWriteFunction, //optional
transformRead: myTransformReadFunction, //optional
maxTries: 1, // optional, default 5
chunkSize: 1024*1024 // optional, default GridFS chunk size in bytes (can be overridden per file).
// Default: 2MB. Reasonable range: 512KB - 4MB
});
Images = new FS.Collection("images", {
stores: [imageStore]
});
// Add file reference of the event photo to the event
var file = $('#file').get(0).files[0];
var fileObj = eventPhotos.insert(file);
events.insert({
name: 'My Event',
photo: fileObj
});
———————————————————————————
// Later: Retrieve the event with the photo
var event = events.findOne({name: 'My Event'});
// This loads the data of the photo into event.photo
// You can include it in your collection transform function.
event.photo.getFileRecord();
保存したファイルの参照をするために、
・ファイルの_idを保存してあげる
・ファイルオブジェクトごと保存してあげる
追記)ファイルオブジェクトごと保存は注意!
必要となる情報を.url()や.name()などで分けて保存を推奨
もし入れる場合は、meteor add cfs:ejson-file を実行すること
var storeFiles = new FS.Store.GridFS("files");
var Files = new FS.Collection("files", {
stores: [
storeFiles
]
});
storeFiles.on('stored', function(storeName, fileObj){
// do something
});
ファイルアップロードの完了検知
$ brew install graphicsmagick
$ meteor add cfs:graphicsmagick
var createThumb = function(fileObj, readStream, writeStream) {
// Transform the image into a 10x10px thumbnail
gm(readStream, fileObj.name()).resize('10', '10').stream().pipe(writeStream);
};
Images = new FS.Collection("images", {
stores: [
//you should write it top of list if you want to show little size image first
new FS.Store.GridFS("thumbs", { transformWrite: createThumb }),
new FS.Store.GridFS("images"),
],
filter: {
allow: {
contentTypes: ['image/*'] //allow only images in this FS.Collection
}
}
});
Template.imageView.helpers({
images: function () {
return Images.find(); // Where Images is an FS.Collection instance
}
});
<template name="imageView">
<div class="imageView">
{{#each images}}
<div>
<a href="{{this.url}}" target="_blank">
<img src="{{this.url store='thumbs' uploading='/images/uploading.gif' storing='/images/storing.gif'}}"
alt="" class="thumbnail" />
</a>
</div>
{{/each}}
</div>
</template>
$ meteor add cfs:ui
{{#with FS.GetFile "images" selectedImageId}}
<img src="{{this.url store='thumbnails'}}" alt="">
{{/with}}
{{#each images}}
Delete {{this.name}}: {{#FS.DeleteButton class="btn btn-danger btn-xs"}}Delete Me{{/FS.DeleteButton}}
{{/each}}
{{#each images}}
{{#unless this.isUploaded}}
{{> FS.UploadProgressBar bootstrap=true
class='progress-bar-success progress-bar-striped active'
showPercent=true}}
{{/unless}}
{{/each}}
Helpers
https://github.com/CollectionFS/Meteor-cfs-ui
Template.files.events({
'dropped .imageArea': FS.EventHandlers.insertFiles(Images, {
metadata: function (fileObj) {
return {
owner: Meteor.userId(),
foo: "bar"
};
},
after: function (error, fileObj) {
console.log("Inserted", fileObj.name);
}
}),
'change #imageInput': FS.EventHandlers.insertFiles(Images, {
metadata: function (fileObj) {
return {
owner: Meteor.userId(),
foo: "bar"
};
},
after: function (error, fileObj) {
console.log("Inserted", fileObj.name);
}
}),
});
Event Handler Creators
navigator.getUserMedia({
audio: setAudio, //true or false
video: setVideo //true or false
}, function(stream) {
mediaStream = stream;
if(setAudio) {
recordAudio = RecordRTC(stream, {
bufferSize: 16384
});
recordAudio.startRecording();
}
if(setVideo) {
if (!isFirefox) {
recordVideo = RecordRTC(stream, {
type: 'video'
});
recordVideo.startRecording();
}
}
}, function(error) {
alert(JSON.stringify(error));
});
// blob, DataURL, etc.
recordVideo.stopRecording(function() {
videoFile = new File([recordVideo.getBlob()], tmpFileName + '.webm', {type:'video/webm'});
});
サーバ側でのvideo, audioファイルのマージ部分でドハマリ
別プロセスで実行させる・・・非同期・・・嫌な予感
Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.
Fiber?
Meteor.bindEnvironment?
var boundFunction = Meteor.bindEnvironment(function(){
log('hello');
}, function(e) {
throw e;
});
setTimeout(boundFunction, 5000);