Repository: opsdroid/opsdroid

Issue:
opdroid error when try send an pdf in matrix
<!-- Before you post an issue or if you are unsure about something join our matrix channel https://app.element.io/#/room/#opsdroid-general:matrix.org and ask away! We are more than happy to help you. -->
# Description
Please include a summary of the issue.
Try to send an pdf file with opsdroid in matrix
Use code from https://github.com/Tyagdit/opsdroid-demo-skill
My code is

    @match_regex(r'acord', case_sensitive=False)
    async def send_file(self, message):
        with open("/home/mike/documente/acord.pdf", "rb") as send_file:
            #pdfReader=PyPDF2.PdfFileReader(send_file)
            send_file = send_file.read()
            send_event = File(name="file", file_bytes=send_file, mimetype="application/x-pdf")
        #send_event = File(name="pic", url="https://docs.opsdroid.dev/en/stable/_static/logo.png", mimetype="image/png")
        await message.respond(send_event)


## Steps to Reproduce
Please also include relevant information and steps to reproduce the bug/issue.
when type acord
obtain this error in opsdroid
ERROR opsdroid.connector.matrix.connector: Error while sending the file. Reason: Request must specify a Content-Length (status code M_UNKNOWN)

## Expected Functionality
Explain what should happen.


## Experienced Functionality
Explain what happened instead(Please include the debug log).

## Versions
- **Opsdroid version:** latest
- **Python version:** 3.9
- **OS/Docker version:**

## Configuration File
Please include your version of the configuration file below.

```yaml
# Your code goes here.

```

## Additional Details
Any other details you wish to include such as screenshots, console messages, etc.


<!-- Love opsdroid? Please consider supporting our collective:
 +👉  https://opencollective.com/opsdroid/donate -->

opdroid error when try send an pdf in matrix
<!-- Before you post an issue or if you are unsure about something join our matrix channel https://app.element.io/#/room/#opsdroid-general:matrix.org and ask away! We are more than happy to help you. -->
# Description
Please include a summary of the issue.
Try to send an pdf file with opsdroid in matrix
Use code from https://github.com/Tyagdit/opsdroid-demo-skill
My code is

    @match_regex(r'acord', case_sensitive=False)
    async def send_file(self, message):
        with open("/home/mike/documente/acord.pdf", "rb") as send_file:
            #pdfReader=PyPDF2.PdfFileReader(send_file)
            send_file = send_file.read()
            send_event = File(name="file", file_bytes=send_file, mimetype="application/x-pdf")
        #send_event = File(name="pic", url="https://docs.opsdroid.dev/en/stable/_static/logo.png", mimetype="image/png")
        await message.respond(send_event)


## Steps to Reproduce
Please also include relevant information and steps to reproduce the bug/issue.
when type acord
obtain this error in opsdroid
ERROR opsdroid.connector.matrix.connector: Error while sending the file. Reason: Request must specify a Content-Length (status code M_UNKNOWN)

## Expected Functionality
Explain what should happen.


## Experienced Functionality
Explain what happened instead(Please include the debug log).

## Versions
- **Opsdroid version:** latest
- **Python version:** 3.9
- **OS/Docker version:**

## Configuration File
Please include your version of the configuration file below.

```yaml
# Your code goes here.

```

## Additional Details
Any other details you wish to include such as screenshots, console messages, etc.


<!-- Love opsdroid? Please consider supporting our collective:
 +👉  https://opencollective.com/opsdroid/donate -->



Discussion:
Well ok that's weird. I am kinda surprised this hasn't been an issue before. What homeserver are you using?
Synapse with postgres
This is config for opsdroid
```
  ## Matrix (core)
  matrix:
#    # Required
    mxid: "@robot:matrix.somedomain"
    password: "somepassword"
#    # A dictionary of multiple rooms
#    # One of these should be named 'main'
    rooms:
      'main': "#ocpisv:matrix.somedomain"
#      'other': '#element-web:matrix.org'
#    # Optional
    homeserver: "https://matrix.somedomain"
    nick: "Robot"  # The nick will be set on startup
    enable_encryption: True
#  room_specific_nicks: True  # Look up room specific nicknames of senders (expensive in large rooms)
    device_name: "opsdroid"
    device_id: "opsdroid" # A unique string to use as an ID for a persistent opsdroid device
#    store_path: "path/to/store/" # Path to the directory where the matrix store will be saved
#
```
Do you happen to know if your synapse is using the built in media store, or if it's using a third party media store?
Also can you send images ok?
don't try with png, but at jpg is same error. (mimetype images/jpg)
I will try tomorow with an png and update this
Do you happen to know if your synapse is using the built in media store, or if it's using a third party media store?
this is from homeserver
media_store_path: "/var/lib/matrix-synapse/media"

with matrix there is no pb to send an receive files
limitation at 10MB(if count)
file is 4MB dimension (but jpg have same KB)
Same error with png
I think I see a issue
at opsdroid -configuration.yaml 
i see this line
#    store_path: "path/to/store/" # Path to the directory where the matrix store will be saved

In matrix, I have default location for saving files, but matrix, and opsdroid aren't running on same machine, so opsdroid never can't reach matrix storelocation.


      


They are different `store_paths`, the opsdroid store path stores a database of cryptographic keys when end to end encryption is enabled. This is presumably because of a recent change to synapse.
Well ok that's weird. I am kinda surprised this hasn't been an issue before. What homeserver are you using?
Synapse with postgres
This is config for opsdroid
```
  ## Matrix (core)
  matrix:
#    # Required
    mxid: "@robot:matrix.somedomain"
    password: "somepassword"
#    # A dictionary of multiple rooms
#    # One of these should be named 'main'
    rooms:
      'main': "#ocpisv:matrix.somedomain"
#      'other': '#element-web:matrix.org'
#    # Optional
    homeserver: "https://matrix.somedomain"
    nick: "Robot"  # The nick will be set on startup
    enable_encryption: True
#  room_specific_nicks: True  # Look up room specific nicknames of senders (expensive in large rooms)
    device_name: "opsdroid"
    device_id: "opsdroid" # A unique string to use as an ID for a persistent opsdroid device
#    store_path: "path/to/store/" # Path to the directory where the matrix store will be saved
#
```
Do you happen to know if your synapse is using the built in media store, or if it's using a third party media store?
Also can you send images ok?
don't try with png, but at jpg is same error. (mimetype images/jpg)
I will try tomorow with an png and update this
Do you happen to know if your synapse is using the built in media store, or if it's using a third party media store?
this is from homeserver
media_store_path: "/var/lib/matrix-synapse/media"

with matrix there is no pb to send an receive files
limitation at 10MB(if count)
file is 4MB dimension (but jpg have same KB)
Same error with png
I think I see a issue
at opsdroid -configuration.yaml 
i see this line
#    store_path: "path/to/store/" # Path to the directory where the matrix store will be saved

In matrix, I have default location for saving files, but matrix, and opsdroid aren't running on same machine, so opsdroid never can't reach matrix storelocation.


      


They are different `store_paths`, the opsdroid store path stores a database of cryptographic keys when end to end encryption is enabled. This is presumably because of a recent change to synapse.

Resolving patch:
diff --git a/opsdroid/connector/matrix/connector.py b/opsdroid/connector/matrix/connector.py
--- a/opsdroid/connector/matrix/connector.py
+++ b/opsdroid/connector/matrix/connector.py
@@ -514,7 +514,10 @@ async def _file_to_mxc_url(self, file_event):
             mimetype = await file_event.get_mimetype()
 
             response = await self.connection.upload(
-                lambda x, y: upload_file, content_type=mimetype, encrypt=encrypt_file
+                lambda x, y: upload_file,
+                content_type=mimetype,
+                encrypt=encrypt_file,
+                filesize=len(upload_file),
             )
 
             response, file_dict = response


Test patch: