-
Notifications
You must be signed in to change notification settings - Fork 1
/
assistant.ts
60 lines (47 loc) · 1.95 KB
/
assistant.ts
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
/// <reference path="typings/main.d.ts" />
import azure_ = require("azure-storage");
import os_ = require("os");
import util_ = require("util");
var eol = os_.EOL || '\n';
export class AzureAppendBlobAssistant {
// check blob properties for the first time access
private _checked: boolean;
constructor(private blob: azure_.BlobService, private container: string, private blobName: string) {
this._checked = false;
}
/**
*
*/
public writeText(
text: string,
callback: azure_.ErrorOrResult<azure_.BlobService.BlobResult>): void {
var self = this;
var cb: azure_.ErrorOrResult<azure_.BlobService.BlobResult> = function (e, r, res) {
setTimeout(callback, 0, e, r, res);
}
text = text + eol;
if (self._checked) return self.blob.appendFromText(self.container, self.blobName, text, cb);
// create or write
self.blob.getBlobProperties(self.container, self.blobName, function (error, result, response) {
self._checked = true;
if (error != null) {
// blob not exist, create it
if (response != null && response.statusCode == 404) {
return self.blob.createAppendBlobFromText(self.container, self.blobName, text, cb);
}
// other error
return cb(error, result, response);
} else {
// check whether it is an append-blob
if (result != null && result.blobType !== "AppendBlob") {
var msg = util_.format(
"Azure Storage {container=%s, blob=%s} is not an append blob but [%s]"
, self.container
, self.blobName);
return cb(new TypeError(msg), null, null);
}
return self.blob.appendFromText(self.container, self.blobName, text, cb);
}
});
}
}