Skip to content

Commit

Permalink
add fake form to download tables export
Browse files Browse the repository at this point in the history
Signed-off-by: grnd-alt <salimbelakkaf@outlook.de>
  • Loading branch information
grnd-alt committed Jul 24, 2024
1 parent dac1643 commit 9255651
Show file tree
Hide file tree
Showing 7 changed files with 388 additions and 24 deletions.
31 changes: 26 additions & 5 deletions lib/Controller/Api1Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,35 @@ public function createTable(string $title, ?string $emoji, string $template = 'c

/**
* returns table scheme
* @param int $tableId
* @return \OCP\AppFramework\Http\DataResponse
*
* @NoAdminRequired
* @CORS
* @NoCSRFRequired
*
* @param int $tableId Table ID
* @return DataResponse<Http::STATUS_OK, TablesTable, array{'Content-Disposition'?:string,'Content-Type'?:string}>|DataResponse<Http::STATUS_FORBIDDEN|Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NOT_FOUND, array{message: string}, array{}>
*
* 200: Table returned
* 403: No permissions
* 404: Not found
*/
public function showScheme(int $tableId): DataResponse {
return $this->handleError(function () use ($tableId) {
try {
$scheme = $this->tableService->getScheme($tableId);
return new DataResponse($scheme->jsonSerialize(), http::STATUS_OK, ["Content-Disposition" => "attachment", "filename" => $scheme->getTitle() . ".json", "Content-Type" => "application/octet-stream"]);
});
return new DataResponse($scheme->jsonSerialize(), http::STATUS_OK, ['Content-Disposition' => 'attachment; filename="'.$scheme->getTitle() . '.json"', 'Content-Type' => 'application/octet-stream']);
} catch (PermissionError $e) {
$this->logger->warning('A permission error occurred: ' . $e->getMessage());
$message = ['message' => $e->getMessage()];
return new DataResponse($message, Http::STATUS_FORBIDDEN);
} catch (NotFoundError $e) {
$this->logger->warning('A not found error occurred: ' . $e->getMessage());
$message = ['message' => $e->getMessage()];
return new DataResponse($message, Http::STATUS_NOT_FOUND);
} catch (InternalError|Exception $e) {
$this->logger->warning('An internal error or exception occurred: '.$e->getMessage());
$message = ['message' => $e->getMessage()];
return new DataResponse($message, Http::STATUS_INTERNAL_SERVER_ERROR);
}
}

/**
Expand Down
2 changes: 0 additions & 2 deletions lib/Controller/ApiTablesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace OCA\Tables\Controller;

use Exception;
use OCA\Tables\Db\Column;
use OCA\Tables\Db\View;
use OCA\Tables\Errors\InternalError;
use OCA\Tables\Errors\NotFoundError;
use OCA\Tables\Errors\PermissionError;
Expand Down
7 changes: 3 additions & 4 deletions lib/Controller/TableScheme.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
<?php

namespace OCA\Tables\Controller;
namespace OCA\Tables\Model;

use JsonSerializable;
use OCA\Tables\Db\Column;
use OCA\Tables\Db\View;
use OCP\App\IAppManager;

class TableScheme implements JsonSerializable {

Expand All @@ -20,8 +19,8 @@ class TableScheme implements JsonSerializable {
protected ?string $description = null;
protected ?string $tablesVersion = null;

public function __construct(string $title, string $emoji, array $columns, array $view, string $description, IAppManager $appManager) {
$this->tablesVersion = $appManager->getAppVersion("tables");
public function __construct(string $title, string $emoji, array $columns, array $view, string $description, string $tablesVersion) {
$this->tablesVersion = $tablesVersion;
$this->title = $title;
$this->emoji = $emoji;
$this->columns = $columns;
Expand Down
4 changes: 2 additions & 2 deletions lib/Service/TableService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use DateTime;
use OCA\Tables\AppInfo\Application;
use OCA\Tables\Controller\TableScheme;
use OCA\Tables\Db\Table;
use OCA\Tables\Db\TableMapper;
use OCA\Tables\Errors\InternalError;
Expand All @@ -15,6 +14,7 @@
use OCA\Tables\Event\TableDeletedEvent;
use OCA\Tables\Event\TableOwnershipTransferredEvent;
use OCA\Tables\Helper\UserHelper;
use OCA\Tables\Model\TableScheme;
use OCA\Tables\ResponseDefinitions;
use OCP\App\IAppManager;
use OCP\AppFramework\Db\DoesNotExistException;
Expand Down Expand Up @@ -551,7 +551,7 @@ public function search(string $term, int $limit = 100, int $offset = 0, ?string
public function getScheme(int $id): TableScheme {
$columns = $this->columnService->findAllByTable($id);
$table = $this->find($id);
return new TableScheme($table->getTitle(), $table->getEmoji(), $columns, $table->getViews(), $table->getDescription(), $this->appManager);
return new TableScheme($table->getTitle(), $table->getEmoji(), $columns, $table->getViews(), $table->getDescription(), $this->appManager->getAppVersion("tables"));
}

// PRIVATE FUNCTIONS ---------------------------------------------------------------
Expand Down
99 changes: 99 additions & 0 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,105 @@
}
}
},
"/index.php/apps/tables/api/1/tables/{tableId}/scheme": {
"get": {
"operationId": "api1-show-scheme",
"summary": "returns table scheme",
"tags": [
"api1"
],
"security": [
{
"basic_auth": []
}
],
"parameters": [
{
"name": "tableId",
"in": "path",
"description": "Table ID",
"required": true,
"schema": {
"type": "integer",
"format": "int64"
}
}
],
"responses": {
"200": {
"description": "Table returned",
"headers": {
"Content-Disposition": {
"schema": {
"type": "string"
}
}
},
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Table"
}
}
}
},
"403": {
"description": "No permissions",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"message"
],
"properties": {
"message": {
"type": "string"
}
}
}
}
}
},
"500": {
"description": "",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"message"
],
"properties": {
"message": {
"type": "string"
}
}
}
}
}
},
"404": {
"description": "Not found",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"message"
],
"properties": {
"message": {
"type": "string"
}
}
}
}
}
}
}
}
},
"/index.php/apps/tables/api/1/tables/{tableId}/views": {
"get": {
"operationId": "api1-index-views",
Expand Down
26 changes: 15 additions & 11 deletions src/modules/navigation/partials/NavigationTableItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,13 @@ import StarOutline from 'vue-material-design-icons/StarOutline.vue'
import ArchiveArrowDown from 'vue-material-design-icons/ArchiveArrowDown.vue'
import ArchiveArrowUpOutline from 'vue-material-design-icons/ArchiveArrowUpOutline.vue'
import permissionsMixin from '../../../shared/components/ncTable/mixins/permissionsMixin.js'
import { getCurrentUser } from '@nextcloud/auth'
import { getCurrentUser, getRequestToken } from '@nextcloud/auth'
import Connection from 'vue-material-design-icons/Connection.vue'
import Import from 'vue-material-design-icons/Import.vue'
import NavigationViewItem from './NavigationViewItem.vue'
import PlaylistPlus from 'vue-material-design-icons/PlaylistPlus.vue'
import IconRename from 'vue-material-design-icons/Rename.vue'
import axios from '@nextcloud/axios'
import { generateOcsUrl } from '@nextcloud/router'
import { generateUrl } from '@nextcloud/router'
export default {
Expand Down Expand Up @@ -246,14 +245,19 @@ export default {
},
exportFile() {
axios.get(generateOcsUrl(`/apps/tables/api/2/tables/scheme/${this.table.id}`)).then(res => {
const blob = new Blob([JSON.stringify(res.data.ocs.data)], { type: 'application/json' })
const link = document.createElement('a')
link.href = URL.createObjectURL(blob)
link.download = this.table.title
link.click()
URL.revokeObjectURL(link.href)
})
const form = document.createElement('form')
form.method = 'GET'
form.action = generateUrl(`/apps/tables/api/1/tables/${this.table.id}/scheme`)
const token = document.createElement('input')
token.type = 'hidden'
token.name = 'requesttoken'
token.value = getRequestToken()
form.appendChild(token)
document.body.appendChild(form)
form.submit()
},
async actionShowShare() {
Expand Down
Loading

0 comments on commit 9255651

Please sign in to comment.