-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
659 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* This file is part of OpenReception | ||
Copyright (C) 2016-, BitStackers K/S | ||
This is free software; you can redistribute it and/or modify it | ||
under terms of the GNU General Public License as published by the | ||
Free Software Foundation; either version 3, or (at your option) any | ||
later version. This software is distributed in the hope that it will be | ||
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
You should have received a copy of the GNU General Public License along with | ||
this program; see the file COPYING3. If not, see http://www.gnu.org/licenses. | ||
*/ | ||
|
||
library openreception.filestore; | ||
|
||
import 'dart:async'; | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:logging/logging.dart'; | ||
|
||
import 'model.dart' as model; | ||
import 'storage.dart' as storage; | ||
|
||
part 'filestore/filestore-git_engine.dart'; | ||
part 'filestore/filestore-ivr.dart'; | ||
part 'filestore/filestore-reception_dialplan.dart'; | ||
|
||
const String libraryName = 'openreception.filestore'; | ||
|
||
final JsonEncoder _jsonpp = new JsonEncoder.withIndent(' '); | ||
|
||
final model.User _systemUser = new model.User.empty() | ||
..name = 'System' | ||
..address = 'root@localhost'; | ||
|
||
/** | ||
* Generate an author string. | ||
*/ | ||
String _authorString(model.User user) => | ||
new HtmlEscape(HtmlEscapeMode.ATTRIBUTE).convert('${user.name}') + | ||
' <${user.address}>'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* This file is part of OpenReception | ||
Copyright (C) 2016-, BitStackers K/S | ||
This is free software; you can redistribute it and/or modify it | ||
under terms of the GNU General Public License as published by the | ||
Free Software Foundation; either version 3, or (at your option) any | ||
later version. This software is distributed in the hope that it will be | ||
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
You should have received a copy of the GNU General Public License along with | ||
this program; see the file COPYING3. If not, see http://www.gnu.org/licenses. | ||
*/ | ||
|
||
part of openreception.filestore; | ||
|
||
class Calendar implements storage.Calendar { | ||
final Logger _log = new Logger('$libraryName.Calendar'); | ||
final String path; | ||
GitEngine _git; | ||
|
||
Future get initialized => _git.initialized; | ||
Future get ready => _git.whenReady; | ||
|
||
/** | ||
* | ||
*/ | ||
Ivr({String this.path: 'json-data/ivr'}) { | ||
_git = new GitEngine(path); | ||
_git.init(); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
Future<model.IvrMenu> create(model.IvrMenu menu, [model.User user]) async { | ||
final File file = new File('$path/${menu.name}.json'); | ||
|
||
if (file.existsSync()) { | ||
throw new storage.ClientError( | ||
'File already exists, please update instead'); | ||
} | ||
|
||
/// Set the user | ||
if (user == null) { | ||
user = _systemUser; | ||
} | ||
|
||
file.writeAsStringSync(_jsonpp.convert(menu)); | ||
|
||
await _git.add(file, 'Added ${menu.name}', _authorString(user)); | ||
|
||
return menu; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
Future<model.IvrMenu> get(String menuName) async { | ||
final File file = new File('$path/${menuName}.json'); | ||
|
||
if (!file.existsSync()) { | ||
throw new storage.NotFound('No file with name ${menuName}'); | ||
} | ||
|
||
try { | ||
final model.IvrMenu menu = | ||
model.IvrMenu.decode(JSON.decode(file.readAsStringSync())); | ||
return menu; | ||
} catch (e) { | ||
throw e; | ||
} | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
Future<Iterable<model.IvrMenu>> list() async => new Directory(path) | ||
.listSync() | ||
.where((fse) => fse is File && fse.path.endsWith('.json')) | ||
.map((File fse) => | ||
model.IvrMenu.decode(JSON.decode(fse.readAsStringSync()))); | ||
|
||
/** | ||
* | ||
*/ | ||
Future<model.IvrMenu> update(model.IvrMenu menu, [model.User user]) async { | ||
final File file = new File('$path/${menu.name}.json'); | ||
|
||
if (!file.existsSync()) { | ||
throw new storage.NotFound(); | ||
} | ||
|
||
/// Set the user | ||
if (user == null) { | ||
user = _systemUser; | ||
} | ||
|
||
file.writeAsStringSync(_jsonpp.convert(menu)); | ||
|
||
await _git._commit('Updated ${menu.name}', _authorString(user)); | ||
|
||
return menu; | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
Future remove(String menuName, [model.User user]) async { | ||
final File file = new File('$path/${menuName}.json'); | ||
|
||
if (!file.existsSync()) { | ||
throw new storage.NotFound(); | ||
} | ||
|
||
/// Set the user | ||
if (user == null) { | ||
user = _systemUser; | ||
} | ||
|
||
await _git.remove(file, 'Removed $menuName', _authorString(user)); | ||
} | ||
} |
Oops, something went wrong.