Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose image api for other clients as api v1.4 #1123

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@
'url' => '/api/{apiVersion}/settings',
'verb' => 'GET',
'requirements' => [
'apiVersion' => '(v1)',
'apiVersion' => '(v1|v1.4)',
],
],
[
Expand All @@ -197,4 +197,22 @@
'path' => '.+',
],
],
[
'name' => 'notes_api#getAttachment',
'url' => '/api/{apiVersion}/attachment/{noteid}',
'verb' => 'GET',
'requirements' => [
'apiVersion' => '(v1.4)',
'noteid' => '\d+'
],
],
[
'name' => 'notes_api#uploadFile',
'url' => '/api/{apiVersion}/attachment/{noteid}',
'verb' => 'POST',
'requirements' => [
'apiVersion' => '(v1.4)',
'noteid' => '\d+'
],
],
]];
61 changes: 54 additions & 7 deletions docs/api/v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ In this document, the Notes API major version 1 and all its minor versions are d

## Minor versions

| API version | Introduced with app version | Remarkable Changes |
|:-----------:|:----------------------------|:-------------------|
| **1.0** | Notes 3.3 (May 2020) | Separate title, no auto rename based on content |
| **1.1** | Notes 3.4 (May 2020) | Filter "Get all notes" by category |
| **1.2** | Notes 4.1 (June 2021) | Preventing lost updates, read-only notes, settings |
| **1.3** | Notes 4.5 (August 2022) | Allow custom file suffixes |
| API version | Introduced with app version | Remarkable Changes |
|:-----------:|:----------------------------|:---------------------------------------------------|
| **1.0** | Notes 3.3 (May 2020) | Separate title, no auto rename based on content |
| **1.1** | Notes 3.4 (May 2020) | Filter "Get all notes" by category |
| **1.2** | Notes 4.1 (June 2021) | Preventing lost updates, read-only notes, settings |
| **1.3** | Notes 4.5 (August 2022) | Allow custom file suffixes |
| **1.4** | Notes 4.9 (November 2023) | Add external image api |



Expand Down Expand Up @@ -132,7 +133,7 @@ Note not found.
<details><summary>Details</summary>

#### Request parameters
- **Body**: some or all "read/write" attributes (see section [Note attributes](#note-attributes)), example:
- **Body**: some or all "read/write" attributes (see section [Note attributes](#note-attributes)), example:
```js
{
"title": "New note",
Expand Down Expand Up @@ -274,6 +275,52 @@ No valid authentication credentials supplied.



### Get attachment (`GET /attachment/{id}`)
<details><summary>Details</summary>

*(since API v1.4)*

#### Request parameters
| Parameter | Type | Description |
|:----------|:------------------------|:-----------------------------------|
| `path` | string, required (path) | Path or name of the image to load. |

#### Response
##### 200 OK
- **Body**: Image or File

##### 400 Bad Request
Endpoint not supported by installed notes app version (requires API version 1.4).

##### 401 Unauthorized
No valid authentication credentials supplied.
</details>


### Put attachment (`POST /attachment/{id}`)
<details><summary>Details</summary>

*(since API v1.4)*

#### Request parameters
None.

#### Response
##### 200 OK
- **Body**: Filename in json encoded:
```js
{
"filename": "image.jpg"
}
```

##### 400 Bad Request
Endpoint not supported by installed notes app version (requires API version 1.4).

##### 401 Unauthorized
No valid authentication credentials supplied.
</details>

## Preventing lost updates and conflict solution

While changing a note using a Notes client, the same note may be changed by another client.
Expand Down
2 changes: 1 addition & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class Application extends App implements IBootstrap {
public const APP_ID = 'notes';
public static array $API_VERSIONS = [ '0.2', '1.3' ];
public static array $API_VERSIONS = [ '0.2', '1.3', '1.4' ];

public function __construct(array $urlParams = []) {
parent::__construct(self::APP_ID, $urlParams);
Expand Down
51 changes: 50 additions & 1 deletion lib/Controller/NotesApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,32 @@
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\StreamResponse;
use OCP\Files\IMimeTypeDetector;
use OCP\IRequest;

class NotesApiController extends ApiController {
private NotesService $service;
private MetaService $metaService;
private SettingsService $settingsService;
private Helper $helper;
private IMimeTypeDetector $mimeTypeDetector;

public function __construct(
string $AppName,
IRequest $request,
NotesService $service,
MetaService $metaService,
SettingsService $settingsService,
Helper $helper
Helper $helper,
IMimeTypeDetector $mimeTypeDetector
) {
parent::__construct($AppName, $request);
$this->service = $service;
$this->metaService = $metaService;
$this->settingsService = $settingsService;
$this->helper = $helper;
$this->mimeTypeDetector = $mimeTypeDetector;
}


Expand Down Expand Up @@ -253,4 +258,48 @@
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
});
}



/**
* With help from: https://github.com/nextcloud/cookbook
* @NoAdminRequired
* @CORS
* @NoCSRFRequired
* @return JSONResponse|StreamResponse
*/
public function getAttachment(int $noteid, string $path): Http\Response {
try {
$targetimage = $this->service->getAttachment(
$this->helper->getUID(),
$noteid,
$path
);
$response = new StreamResponse($targetimage->fopen('rb'));
$response->addHeader('Content-Disposition', 'attachment; filename="' . rawurldecode($targetimage->getName()) . '"');

Check warning on line 279 in lib/Controller/NotesApiController.php

View workflow job for this annotation

GitHub Actions / lint-php (min)

Line exceeds 120 characters; contains 128 characters

Check warning on line 279 in lib/Controller/NotesApiController.php

View workflow job for this annotation

GitHub Actions / lint-php (max)

Line exceeds 120 characters; contains 128 characters
$response->addHeader('Content-Type', $this->mimeTypeDetector->getSecureMimeType($targetimage->getMimeType()));

Check warning on line 280 in lib/Controller/NotesApiController.php

View workflow job for this annotation

GitHub Actions / lint-php (min)

Line exceeds 120 characters; contains 122 characters

Check warning on line 280 in lib/Controller/NotesApiController.php

View workflow job for this annotation

GitHub Actions / lint-php (max)

Line exceeds 120 characters; contains 122 characters
$response->addHeader('Cache-Control', 'public, max-age=604800');
return $response;
} catch (\Exception $e) {
$this->helper->logException($e);
return $this->helper->createErrorResponse($e, Http::STATUS_NOT_FOUND);
}
}

/**
* @NoAdminRequired
* @CORS
* @NoCSRFRequired
*/
public function uploadFile(int $noteid): JSONResponse {
$file = $this->request->getUploadedFile('file');
return $this->helper->handleErrorResponse(function () use ($noteid, $file) {
return $this->service->createImage(
$this->helper->getUID(),
$noteid,
$file
);
});
}

}

Check failure on line 305 in lib/Controller/NotesApiController.php

View workflow job for this annotation

GitHub Actions / lint-php (min)

The closing brace for the class must go on the next line after the body

Check failure on line 305 in lib/Controller/NotesApiController.php

View workflow job for this annotation

GitHub Actions / lint-php (max)

The closing brace for the class must go on the next line after the body
Loading