- alfresco-js-api
- REST API
- alfresco-js-apiを活用できるものは極力活用する
- alfresco-js-apiではファイルアップロードができないため、REST APIを使用する
サンプル
ログイン/ログアウト
alfresco-login.js
let HOST = 'http://192.168.0.XXX:8080';
var AlfrescoApi = require('alfresco-js-api');
this.alfrescoJsApi = new AlfrescoApi({
hostEcm: HOST
});
var self = this;
const userId = 'USER';
const pswd = 'PSWD';
let ticket = null;
self.alfrescoJsApi.login(userId, pswd).then(function(data) {
ticket = data; console.log(ticket);
var isLoggedIn = self.alfrescoJsApi.isLoggedIn();
console.log('Status=', isLoggedIn);
self.alfrescoJsApi.logout()
.then(
function (data) { console.log('Successfully Logout'); },
function (error) { console.error('Possible ticket already expired');});
});
- 実行結果
console
$ node index.js
$ node alfresco-login.js
TICKET_4210c302717f585e5d702e1b01d4733b316817eb
LoggedIn= true
ファイルのアップロード
新規作成
alfresco-upload.js
const HOST = 'http://192.168.0.XXX:8080';
const ticket = 'TICKET_4210c302717f585e5d702e1b01d4733b316817eb';
var r = request.post(HOST + '/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children?alf_ticket='+ticket,
function callback(err, httpResponse, body) {
if(err || JSON.parse(body).error) { return console.log('Upload failed : ' + body); }
console.log('Upload success');
})
var form = r.form()
form.append("name", "sample.txt");
form.append("nodeType", "cm:content");
form.append("relativePath", "Sites/patients/documentLibrary");
form.append("filedata",fs.createReadStream('./test/sample.txt'));
既存更新
alfresco-upload-update.js
var node_id = '374b001f-4d8c-4a64-9f3b-b3a59812c5b5';
var url = HOST + '/alfresco/api/-default-/public/alfresco/versions/1/nodes/' + node_id + '/content?alf_ticket=' + ticket;
console.log(url)
fs.createReadStream('./test/sample.txt').pipe(
request.put(url).on('response',
function (response) {
console.log(response.statusCode) // アップロード結果 200成功
console.log(response.headers['content-type'])
}).on('error',
function (err) {
console.log(err);
}));
検索
- QueryはCMISで記述
Query | 説明 |
---|---|
SELECT * FROM cmis:document() | ルートの全ドキュメントを取得 |
SELECT * FROM cmis:document D WHERE CONTAINS(D,'PATH: "/app:company_home//*"') | 〃 |
SELECT cmis:name, cmis:objectId FROM cmis:document ORDER BY cmis:name | nameを昇順にソート |
SELECT * FROM cmis:document where cmis:name LIKE '%.png%'" | PNGのみ検索 |
SELECT * FROM cmis:folder | ルートの全フォルダを取得 |
SELECT F.* FROM cmis:folder F | 〃 |
select * from cmis:folder where contains('PATH:"/app:company_home/*"') | 〃 |
SELECT * FROM cmis:folder WHERE cmis:name='[FOLDER_NAME]' | フォルダを名前で検索 |
index.js
var search_folder = "共有";
self.alfrescoJsApi.search.searchApi.search({
"query": {
"query": "SELECT * FROM cmis:folder WHERE cmis:name='" + search_folder + "'",
"language": "cmis"
}
}).then(
function (data) {
data.list.entries.forEach(function (v, i) {
// console.log(v.entry);
console.log(i, v.entry.id, v.entry.name)
});
},
function (error) {
console.error(error);
}
);
URLの取得
var node_id = '374b001f-4d8c-4a64-9f3b-b3a59812c5b5';
var contentUrl = self.alfrescoJsApi.content.getContentUrl(node_id);
console.log(contentUrl);
// => http://192.168.0.100:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/374b001f-4d8c-4a64-9f3b-b3a59812c5b5/content?attachment=false&alf_ticket=TICKET_aea56a00e22db2c4c75ee76b947298ce0411bf5a
// => このURLからコンテンツをダウンロード可能
サムネイルの取得
index.js
var node_id = '374b001f-4d8c-4a64-9f3b-b3a59812c5b5';
var thumbnailUrl = self.alfrescoJsApi.content.getDocumentThumbnailUrl(node_id);
console.log(thumbnailUrl);
// => http://192.168.0.100:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/374b001f-4d8c-4a64-9f3b-b3a59812c5b5/renditions/doclib/content?attachment=false&alf_ticket=TICKET_aea56a00e22db2c4c75ee76b947298ce0411bf5a
フォルダ作成
- 共有の直下に新しいフォルダが作成される
index.js
self.alfrescoJsApi.nodes.createFolder('[NEW_FOLDER_NAME]').then(
function(data) {
console.log('The folder is created in root');
console.log(data);
}, function(error) {
console.log('Error in creation of this folder or folder already exist' + error);
});
console
The folder is created in root
{ entry:
{ aspectNames: [ 'cm:auditable' ],
createdAt: 2017-05-12T19:56:07.053Z,
isFolder: true,
isFile: false,
createdByUser: { id: 'admin', displayName: 'Administrator' },
modifiedAt: 2017-05-12T19:56:07.053Z,
modifiedByUser: { id: 'admin', displayName: 'Administrator' },
name: 'newFolderName',
id: '2096a2d5-168e-4af8-9f7b-3f190c3b3ea6',
nodeType: 'cm:folder',
parentId: '32d3f65e-4021-446c-a964-015134e9c432' } }
ダウンロード
index.js
self.alfrescoJsApi.core.nodesApi.getFileContent(nodeId).then(function(data) {
console.log(data);
fs.writeFile('./LOCAL/PATH/TO/FILE.ext', data, function(error) {
if (error) {
console.error(error);
return;
}
console.log('The file was downloaded!');
});
}, function(error) {
console.error(error);
});