Skip to content

Conversation

@julien-nc
Copy link
Member

@julien-nc julien-nc commented May 20, 2022

refs #2411

Implementation of the behaviour suggested by @max-nextcloud in #2411 (comment) with which I completely agree ❤️ :

  • 1. If the image url matches .attachments.MD_FILE_ID/FILENAME use FILENAME to get the image with the attachment API.
  • 2. If the image url matches .attachments.OTHER_FILE_ID/FILENAME
    • a. try to retrieve the image with the WebDav API.
    • b. If that fails try to retrieve FILENAME with the attachment API.
    • c. Maybe rewrite the url with the own file id if b. succeeds.
  • 3. If not, use the path with the WebDav API.
  • 4. If that fails check if the url has a fileID param and try to get the file from that. (So we keep the old behavior for old images)

So this PR brings back the relative path in the markdown content's image target. We now write:

![IMAGE_FILE_NAME](.attachments.MARKDOWN_FILE_ID/ENCODED_IMAGE_FILE_NAME)

The problem with 2.a. and 2.b. is that imageUrl is a computed prop in which we would need a non-async (sync?) image loading check. But we can only make async network calls in a computed prop 😁.
There already is a check which depends on imageUrl.
In short, we would need to make deeper changes to ImageView to allow 2.a. and 2.b.

We could discuss that and in the meantime I might come up with a first solution.

@julien-nc julien-nc added enhancement New feature or request feature: formatting Features related to text formatting and node types 2. developing format: markdown labels May 20, 2022
@julien-nc julien-nc added this to the Nextcloud 25 milestone May 20, 2022
@julien-nc julien-nc force-pushed the enh/2411/improve-image-handling branch from 945a811 to 8f7f263 Compare May 20, 2022 12:44
@julien-nc
Copy link
Member Author

@max-nextcloud I found a reasonable solution for 2.a. and 2.b. : move the content of the imageUrl computed prop in a method (called init) which is called in beforeMount (which is the only time and place we loaded the image anyway). This way we can happily call async stuff.

imageUrl is now a data attribute. It is set if the image has been successfully loaded.

The image loading has been moved in a method as well (loadImage). It has an optional error callback that we use for 2.

The init method now handles all the different cases and is more flexible than the old imageUrl computed prop.

@julien-nc julien-nc force-pushed the enh/2411/improve-image-handling branch from b7509a6 to c9d9645 Compare May 20, 2022 13:01
@julien-nc julien-nc marked this pull request as ready for review May 20, 2022 13:01
@julien-nc
Copy link
Member Author

/compile

@julien-nc
Copy link
Member Author

/compile amend

@nextcloud-command nextcloud-command force-pushed the enh/2411/improve-image-handling branch from 059b730 to 6457ce5 Compare May 20, 2022 13:49
@julien-nc julien-nc force-pushed the enh/2411/improve-image-handling branch from 6457ce5 to c2c3425 Compare May 20, 2022 15:14
@julien-nc
Copy link
Member Author

@vinicius73 Thanks for the review. If you have any idea about how to fix the Jest tests. It seems the checks are done before the async stuff is finished.

@julien-nc julien-nc requested a review from vinicius73 May 20, 2022 15:18
@julien-nc
Copy link
Member Author

Let's commit the compiled code in the end to make our lives easier.

So don't forget to compile locally 😉.

Comment on lines 260 to 262
this.loadImage(this.getTextApiUrl(imageFileName)).catch((e) => {
this.onImageLoadFailure()
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simplify this code.

Suggested change
this.loadImage(this.getTextApiUrl(imageFileName)).catch((e) => {
this.onImageLoadFailure()
})
this.loadImage(this.getTextApiUrl(imageFileName))
.catch(this.onImageLoadFailure)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we can use async/await and wrap this.init() and handle error from this.init()

Suggested change
this.loadImage(this.getTextApiUrl(imageFileName)).catch((e) => {
this.onImageLoadFailure()
})
await this.loadImage(this.getTextApiUrl(imageFileName))

or

Suggested change
this.loadImage(this.getTextApiUrl(imageFileName)).catch((e) => {
this.onImageLoadFailure()
})
return this.loadImage(this.getTextApiUrl(imageFileName))

and, change this.init() call.

this.init().catch(this.onImageLoadFailure)

@julien-nc julien-nc force-pushed the enh/2411/improve-image-handling branch from c2c3425 to e7f8e53 Compare May 20, 2022 16:12
@max-nextcloud
Copy link
Collaborator

Thanks a lot for picking up the idea. 😍
I only had a quick look at the code and checked if the text:// urls are still parsed and will still display nicely. Looks good to me in that regard.

@julien-nc
Copy link
Member Author

/compile

Signed-off-by: Julien Veyssier <[email protected]>
@julien-nc julien-nc force-pushed the enh/2411/improve-image-handling branch from 70b408f to 7b8c4e2 Compare May 23, 2022 14:32
@julien-nc
Copy link
Member Author

/compile

@julien-nc julien-nc force-pushed the enh/2411/improve-image-handling branch from cc11bf5 to b95deb9 Compare May 23, 2022 14:36
@julien-nc
Copy link
Member Author

/compile

@julien-nc julien-nc requested a review from vinicius73 May 23, 2022 15:07
@julien-nc
Copy link
Member Author

@vinicius73 @max-nextcloud Ready to merge!

',;:!?.§-_a_',
'a`a`.png',
];
public function testGetOldAttachmentNamesFromContent() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something for a separate PR but we should move those tests into tests/unit/Service/ImageServiceTest.php

Copy link
Member

@juliusknorr juliusknorr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments, but feel free to address them separately :)

@julien-nc julien-nc force-pushed the enh/2411/improve-image-handling branch from f416565 to e78c3ea Compare May 23, 2022 17:11
@julien-nc
Copy link
Member Author

@vinicius73 Thanks
@juliushaertl Better now than forgetting it, thanks.

@julien-nc julien-nc force-pushed the enh/2411/improve-image-handling branch from e78c3ea to 1715a7c Compare May 23, 2022 17:13
@julien-nc
Copy link
Member Author

/compile

Signed-off-by: nextcloud-command <[email protected]>
@julien-nc julien-nc merged commit 395bc8f into master May 23, 2022
@delete-merged-branch delete-merged-branch bot deleted the enh/2411/improve-image-handling branch May 23, 2022 17:31
@julien-nc
Copy link
Member Author

Should we backport this to stable24? I think we should.

@julien-nc
Copy link
Member Author

/backport to stable24

@backportbot-nextcloud
Copy link

The backport to stable24 failed. Please do this backport manually.

julien-nc pushed a commit that referenced this pull request May 24, 2022
@mejo-
Copy link
Member

mejo- commented May 24, 2022

Thanks so much for implementing this @eneiluj ❤️

I think that we can close #2411 then, no?

@max-nextcloud max-nextcloud added the backported successfully backported label Jul 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3. to review backported successfully backported enhancement New feature or request feature: formatting Features related to text formatting and node types format: markdown

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants