forked from bioversity/Crop-Ontology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blobstore.js
90 lines (70 loc) · 2.89 KB
/
blobstore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
importPackage(com.google.appengine.api.blobstore);
importPackage(com.google.appengine.api.files);
var blobstore = {
blobstoreService: BlobstoreServiceFactory.getBlobstoreService(),
createUploadUrl: function(successPath) {
return this.blobstoreService.createUploadUrl(successPath);
},
/**
* read each Blob with key 'blobKey' line by line
* and at each line calls the callback
*/
readLine: function(blobKey, func) {
var reader = new BufferedReader(
new InputStreamReader(new BlobstoreInputStream(blobKey)));
var line = null;
while((line = reader.readLine()) != null) {
func(line);
}
reader.close();
},
/**
* writes a string to the blobstore
* and returns the key reference of it
*/
writeString: function(str) {
// Get a file service
var fileService = FileServiceFactory.getFileService();
// Create a new Blob file with mime-type "text/plain"
var file = fileService.createNewBlobFile("text/plain");
// Open a channel to write to it
var lock = true;
var writeChannel = fileService.openWriteChannel(file, lock);
// Different standard Java ways of writing to the channel
// are possible. Here we use a PrintWriter:
var out = new PrintWriter(java.nio.channels.Channels.newWriter(writeChannel, "UTF8"));
out.print(str);
// Close without finalizing and save the file path for writing later
out.close();
/*
var path = file.getFullPath();
// Write more to the file in a separate request:
file = new AppEngineFile(path);
// This time lock because we intend to finalize
lock = true;
writeChannel = fileService.openWriteChannel(file, lock);
// This time we write to the channel using standard Java
writeChannel.write(java.nio.ByteBuffer.wrap
(new java.lang.String("And miles to go before I sleep.").getBytes()));
*/
// Now finalize
writeChannel.closeFinally();
// Later, read from the file using the file API
/*
lock = false; // Let other people read at the same time
var readChannel = fileService.openReadChannel(file, false);
// Again, different standard Java ways of reading from the channel.
var reader =
new BufferedReader(java.nio.channels.Channels.newReader(readChannel, "UTF8"));
var line = reader.readLine();
// line = "The woods are lovely dark and deep."
readChannel.close();
// Now read from the file using the Blobstore API
var blobKey = fileService.getBlobKey(file);
var segment = new java.lang.String(this.blobstoreService.fetchData(blobKey, 30, 40));
*/
var blobKey = fileService.getBlobKey(file);
return blobKey;
}
};
exports = blobstore;