99use OCA \Deck \Db \Acl ;
1010use OCA \Deck \Db \Board ;
1111use OCA \Deck \Service \BoardService ;
12+ use OCA \Deck \Service \Importer \BoardImportService ;
1213use OCA \Deck \Service \PermissionService ;
1314use OCP \AppFramework \ApiController ;
1415use OCP \AppFramework \Http ;
1516use OCP \AppFramework \Http \DataResponse ;
17+ use OCP \IL10N ;
1618use OCP \IRequest ;
1719
1820class BoardController extends ApiController {
@@ -21,6 +23,8 @@ public function __construct(
2123 IRequest $ request ,
2224 private BoardService $ boardService ,
2325 private PermissionService $ permissionService ,
26+ private BoardImportService $ boardImportService ,
27+ private IL10N $ l10n ,
2428 private $ userId ,
2529 ) {
2630 parent ::__construct ($ appName , $ request );
@@ -162,4 +166,62 @@ public function transferOwner(int $boardId, string $newOwner): DataResponse {
162166 public function export ($ boardId ) {
163167 return $ this ->boardService ->export ($ boardId );
164168 }
169+
170+ /**
171+ * @NoAdminRequired
172+ */
173+ public function import (): DataResponse {
174+ $ file = $ this ->request ->getUploadedFile ('file ' );
175+ $ error = null ;
176+ $ phpFileUploadErrors = [
177+ UPLOAD_ERR_OK => $ this ->l10n ->t ('The file was uploaded ' ),
178+ UPLOAD_ERR_INI_SIZE => $ this ->l10n ->t ('The uploaded file exceeds the upload_max_filesize directive in php.ini ' ),
179+ UPLOAD_ERR_FORM_SIZE => $ this ->l10n ->t ('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form ' ),
180+ UPLOAD_ERR_PARTIAL => $ this ->l10n ->t ('The file was only partially uploaded ' ),
181+ UPLOAD_ERR_NO_FILE => $ this ->l10n ->t ('No file was uploaded ' ),
182+ UPLOAD_ERR_NO_TMP_DIR => $ this ->l10n ->t ('Missing a temporary folder ' ),
183+ UPLOAD_ERR_CANT_WRITE => $ this ->l10n ->t ('Could not write file to disk ' ),
184+ UPLOAD_ERR_EXTENSION => $ this ->l10n ->t ('A PHP extension stopped the file upload ' ),
185+ ];
186+
187+ if (empty ($ file )) {
188+ $ error = $ this ->l10n ->t ('No file uploaded or file size exceeds maximum of %s ' , [\OCP \Util::humanFileSize (\OCP \Util::uploadLimit ())]);
189+ }
190+ if (!empty ($ file ) && array_key_exists ('error ' , $ file ) && $ file ['error ' ] !== UPLOAD_ERR_OK ) {
191+ $ error = $ phpFileUploadErrors [$ file ['error ' ]];
192+ }
193+ if (!empty ($ file ) && $ file ['error ' ] === UPLOAD_ERR_OK && !in_array ($ file ['type ' ], ['application/json ' , 'text/plain ' ])) {
194+ $ error = $ this ->l10n ->t ('Invalid file type. Only JSON files are allowed. ' );
195+ }
196+ if ($ error !== null ) {
197+ return new DataResponse ([
198+ 'status ' => 'error ' ,
199+ 'message ' => $ error ,
200+ ], Http::STATUS_BAD_REQUEST );
201+ }
202+
203+ try {
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+ } catch (\TypeError $ e ) {
216+ return new DataResponse ([
217+ 'status ' => 'error ' ,
218+ 'message ' => $ this ->l10n ->t ('Invalid JSON data ' ),
219+ ], Http::STATUS_BAD_REQUEST );
220+ } catch (\Exception $ e ) {
221+ return new DataResponse ([
222+ 'status ' => 'error ' ,
223+ 'message ' => $ this ->l10n ->t ('Failed to import board ' ),
224+ ], Http::STATUS_BAD_REQUEST );
225+ }
226+ }
165227}
0 commit comments