Skip to content

Commit

Permalink
Now caching uploads before passing them to go. Also added auto-detect…
Browse files Browse the repository at this point in the history
…ion of mime types
  • Loading branch information
Labfox committed Sep 19, 2024
1 parent 62d2ee0 commit 95f7edd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
push:
branches: [ "main" ]
schedule:
- cron: "1 1 1 * *"
- cron: "1 1 * * *"


jobs:
Expand Down
6 changes: 0 additions & 6 deletions whatsfly/dependencies/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,18 +472,13 @@ func (w *WhatsAppClient) UploadFile(path string, kind string, return_id string)
if !w.wpClient.IsConnected() {
err := w.wpClient.Connect()
if err != nil {
fmt.Printf("ah3")
return 1
}
}

// var filedata []byte
filedata, err := os.ReadFile(path)
if err != nil {
fmt.Println("ah1")
fmt.Println(path)
fmt.Println(kind)
fmt.Println(return_id)
return 1
}

Expand All @@ -505,7 +500,6 @@ func (w *WhatsAppClient) UploadFile(path string, kind string, return_id string)
var uploaded whatsmeow.UploadResponse
uploaded, err = w.wpClient.Upload(context.Background(), filedata, mediakind)
if err != nil {
fmt.Printf("ah2")
return 1
}

Expand Down
26 changes: 23 additions & 3 deletions whatsfly/whatsapp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import shutil
import tempfile
import time
import uuid
from typing import Callable
Expand Down Expand Up @@ -26,6 +28,7 @@
import warnings
import functools
import qrcode
import mimetypes

def deprecated(func):
"""This is a decorator which can be used to mark functions
Expand Down Expand Up @@ -323,13 +326,27 @@ def getGroupInfo(


def uploadFile(
self, path: str, kind: str, mimetype: str
self, path: str, kind: str, mimetype: str=None
) -> id:
"""
Get info for a link
:param group: Group id
Uploads a file
:param path: The filepath
:param kind: The kind of the upload. One of: image, video, audio, document
:return: Group information
"""

if mimetype == None:
mimetype = mimetypes.guess_type(path)[0]

if not kind in ["image", "video", "audio", "document"]:
raise Exception("Invalid kind")

temporaryDirectory = tempfile.TemporaryDirectory(ignore_cleanup_errors=True)

tempName = temporaryDirectory.name+"/"+path.split("/")[-1]

shutil.copyfile(path, tempName)

return_uuid = uuid.uuid1()

error = upload_file_wrapper(
Expand All @@ -339,9 +356,12 @@ def uploadFile(
str(return_uuid).encode()
)


while not str(return_uuid) in self._methodReturns:
time.sleep(0.001)

temporaryDirectory.cleanup()

response = self._methodReturns[str(return_uuid)]["return"]

return Upload(int(response), mimetype, kind)
Expand Down

0 comments on commit 95f7edd

Please sign in to comment.