1010use OCA \Deck \Db \Acl ;
1111use OCA \Deck \Db \Board ;
1212use OCA \Deck \Service \BoardService ;
13+ use OCA \Deck \Service \Importer \BoardImportService ;
1314use OCA \Deck \Service \PermissionService ;
1415use OCP \AppFramework \ApiController ;
1516use OCP \AppFramework \Http ;
1617use OCP \AppFramework \Http \DataResponse ;
18+ use OCP \IL10N ;
1719use OCP \IRequest ;
1820
1921class BoardController extends ApiController {
@@ -22,6 +24,8 @@ public function __construct(
2224 IRequest $ request ,
2325 private BoardService $ boardService ,
2426 private PermissionService $ permissionService ,
27+ private BoardImportService $ boardImportService ,
28+ private IL10N $ l10n ,
2529 private $ userId ,
2630 ) {
2731 parent ::__construct ($ appName , $ request );
@@ -163,4 +167,50 @@ public function transferOwner(int $boardId, string $newOwner): DataResponse {
163167 public function export ($ boardId ) {
164168 return $ this ->boardService ->export ($ boardId );
165169 }
170+
171+ /**
172+ * @NoAdminRequired
173+ */
174+ public function import (): DataResponse {
175+ $ file = $ this ->request ->getUploadedFile ('file ' );
176+ $ error = null ;
177+ $ phpFileUploadErrors = [
178+ UPLOAD_ERR_OK => $ this ->l10n ->t ('The file was uploaded ' ),
179+ UPLOAD_ERR_INI_SIZE => $ this ->l10n ->t ('The uploaded file exceeds the upload_max_filesize directive in php.ini ' ),
180+ UPLOAD_ERR_FORM_SIZE => $ this ->l10n ->t ('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form ' ),
181+ UPLOAD_ERR_PARTIAL => $ this ->l10n ->t ('The file was only partially uploaded ' ),
182+ UPLOAD_ERR_NO_FILE => $ this ->l10n ->t ('No file was uploaded ' ),
183+ UPLOAD_ERR_NO_TMP_DIR => $ this ->l10n ->t ('Missing a temporary folder ' ),
184+ UPLOAD_ERR_CANT_WRITE => $ this ->l10n ->t ('Could not write file to disk ' ),
185+ UPLOAD_ERR_EXTENSION => $ this ->l10n ->t ('A PHP extension stopped the file upload ' ),
186+ ];
187+
188+ if (empty ($ file )) {
189+ $ error = $ this ->l10n ->t ('No file uploaded or file size exceeds maximum of %s ' , [\OCP \Util::humanFileSize (\OCP \Util::uploadLimit ())]);
190+ }
191+ if (!empty ($ file ) && array_key_exists ('error ' , $ file ) && $ file ['error ' ] !== UPLOAD_ERR_OK ) {
192+ $ error = $ phpFileUploadErrors [$ file ['error ' ]];
193+ }
194+ if (!empty ($ file ) && $ file ['error ' ] === UPLOAD_ERR_OK && !in_array ($ file ['type ' ], ['application/json ' , 'text/plain ' ])) {
195+ $ error = $ this ->l10n ->t ('Invalid file type. Only JSON files are allowed. ' );
196+ }
197+ if ($ error !== null ) {
198+ return new DataResponse ([
199+ 'status ' => 'error ' ,
200+ 'message ' => $ error ,
201+ ], Http::STATUS_BAD_REQUEST );
202+ }
203+
204+ $ fileContent = file_get_contents ($ file ['tmp_name ' ]);
205+ $ this ->boardImportService ->setSystem ('DeckJson ' );
206+ $ config = new \stdClass ();
207+ $ config ->owner = $ this ->userId ;
208+ $ this ->boardImportService ->setConfigInstance ($ config );
209+ $ this ->boardImportService ->setData (json_decode ($ fileContent ));
210+ $ this ->boardImportService ->import ();
211+ $ importedBoard = $ this ->boardImportService ->getBoard ();
212+ $ board = $ this ->boardService ->find ($ importedBoard ->getId ());
213+
214+ return new DataResponse ($ board , Http::STATUS_OK );
215+ }
166216}
0 commit comments