Skip to content

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

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

Open
wants to merge 2 commits 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 @@ -183,7 +183,7 @@
'url' => '/api/{apiVersion}/settings',
'verb' => 'GET',
'requirements' => [
'apiVersion' => '(v1)',
'apiVersion' => '(v1|v1.4)',
],
],
[
Expand All @@ -203,4 +203,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 @@ -9,12 +9,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 @@ -136,7 +137,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 @@ -278,6 +279,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 @@ -21,7 +21,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
49 changes: 49 additions & 0 deletions lib/Controller/NotesApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
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,
Expand All @@ -33,12 +36,14 @@
MetaService $metaService,
SettingsService $settingsService,
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 @@ -259,4 +264,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'));

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

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable30

PossiblyFalseArgument

lib/Controller/NotesApiController.php:284:35: PossiblyFalseArgument: Argument 1 of OCP\AppFramework\Http\StreamResponse::__construct cannot be false, possibly resource|string value expected (see https://psalm.dev/104)

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

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

PossiblyFalseArgument

lib/Controller/NotesApiController.php:284:35: PossiblyFalseArgument: Argument 1 of OCP\AppFramework\Http\StreamResponse::__construct cannot be false, possibly resource|string value expected (see https://psalm.dev/104)

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

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

PossiblyFalseArgument

lib/Controller/NotesApiController.php:284:35: PossiblyFalseArgument: Argument 1 of OCP\AppFramework\Http\StreamResponse::__construct cannot be false, possibly resource|string value expected (see https://psalm.dev/104)

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

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

PossiblyFalseArgument

lib/Controller/NotesApiController.php:284:35: PossiblyFalseArgument: Argument 1 of OCP\AppFramework\Http\StreamResponse::__construct cannot be false, possibly resource|string value expected (see https://psalm.dev/104)

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

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable31

PossiblyFalseArgument

lib/Controller/NotesApiController.php:284:35: PossiblyFalseArgument: Argument 1 of OCP\AppFramework\Http\StreamResponse::__construct cannot be false, possibly resource|string value expected (see https://psalm.dev/104)
$response->addHeader('Content-Disposition', 'attachment; filename="' . rawurldecode($targetimage->getName()) . '"');
$response->addHeader('Content-Type', $this->mimeTypeDetector->getSecureMimeType($targetimage->getMimeType()));
$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) {

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

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable30

MissingClosureReturnType

lib/Controller/NotesApiController.php:302:45: MissingClosureReturnType: Closure does not have a return type, expecting mixed (see https://psalm.dev/068)

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

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable28

MissingClosureReturnType

lib/Controller/NotesApiController.php:302:45: MissingClosureReturnType: Closure does not have a return type, expecting mixed (see https://psalm.dev/068)

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

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable29

MissingClosureReturnType

lib/Controller/NotesApiController.php:302:45: MissingClosureReturnType: Closure does not have a return type, expecting mixed (see https://psalm.dev/068)

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

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-master

MissingClosureReturnType

lib/Controller/NotesApiController.php:302:45: MissingClosureReturnType: Closure does not have a return type, expecting mixed (see https://psalm.dev/068)

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

View workflow job for this annotation

GitHub Actions / static-psalm-analysis dev-stable31

MissingClosureReturnType

lib/Controller/NotesApiController.php:302:45: MissingClosureReturnType: Closure does not have a return type, expecting mixed (see https://psalm.dev/068)
return $this->service->createImage(
$this->helper->getUID(),
$noteid,
$file
);
});
}

}
Loading