From ceb5b5f671c00bbf769103952dc60606bcd7c23e Mon Sep 17 00:00:00 2001 From: Fawkes100 Date: Tue, 7 Jan 2025 19:35:14 +0100 Subject: [PATCH] Sync Images --- .../core/notevault/data/MusicNoteDAO.java | 8 +++ .../java/core/notevault/data/NoteSheet.java | 12 ++++- .../java/core/notevault/sync/SyncWorker.java | 2 + .../sync/synchronisation/SyncService.java | 10 ++++ .../synchronisation/songs/SongSyncWorker.java | 50 +++++++++++++++++++ .../songs/notesheets/UploadResponse.java | 5 ++ 6 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/core/notevault/sync/synchronisation/songs/notesheets/UploadResponse.java diff --git a/app/src/main/java/core/notevault/data/MusicNoteDAO.java b/app/src/main/java/core/notevault/data/MusicNoteDAO.java index ea09e17..fe32391 100644 --- a/app/src/main/java/core/notevault/data/MusicNoteDAO.java +++ b/app/src/main/java/core/notevault/data/MusicNoteDAO.java @@ -7,6 +7,7 @@ import core.notevault.sync.synchronisation.songs.creation.SongCreationResponse; import java.time.LocalDateTime; import java.util.List; +import java.util.Optional; @Dao public interface MusicNoteDAO { @@ -72,5 +73,12 @@ public interface MusicNoteDAO { @Query("DELETE FROM music_notes WHERE serverID IN (:serverIDs)") void deleteSongs(List serverIDs); + @Query("SELECT * FROM note_sheets ns WHERE ns.syncStatus = :status") + List getNoteSheetsWithStatus(SyncStatus status); + @Query("SELECT mn.serverID FROM music_notes mn WHERE mn.musicNoteId =:musicNoteId") + Optional getMusicNoteByLocalID(long musicNoteId); + + @Update + void updateNoteSheet(NoteSheet noteSheet); } diff --git a/app/src/main/java/core/notevault/data/NoteSheet.java b/app/src/main/java/core/notevault/data/NoteSheet.java index 126d63e..3a5e644 100644 --- a/app/src/main/java/core/notevault/data/NoteSheet.java +++ b/app/src/main/java/core/notevault/data/NoteSheet.java @@ -2,19 +2,21 @@ package core.notevault.data; import androidx.room.Entity; import androidx.room.PrimaryKey; +import core.notevault.data.sync.SyncStatus; @Entity(tableName = "note_sheets") public class NoteSheet { @PrimaryKey(autoGenerate = true) private int id; - + private SyncStatus syncStatus; private long musicNoteId; private String filePath; public NoteSheet(long musicNoteID, String filePath) { this.musicNoteId = musicNoteID; this.filePath = filePath; + this.syncStatus = SyncStatus.CREATED; } public NoteSheet() { @@ -43,4 +45,12 @@ public class NoteSheet { public void setFilePath(String filePath) { this.filePath = filePath; } + + public SyncStatus getSyncStatus() { + return syncStatus; + } + + public void setSyncStatus(SyncStatus syncStatus) { + this.syncStatus = syncStatus; + } } diff --git a/app/src/main/java/core/notevault/sync/SyncWorker.java b/app/src/main/java/core/notevault/sync/SyncWorker.java index 330931c..dc45e0c 100644 --- a/app/src/main/java/core/notevault/sync/SyncWorker.java +++ b/app/src/main/java/core/notevault/sync/SyncWorker.java @@ -41,8 +41,10 @@ public class SyncWorker extends Worker{ songSyncWorker.syncSongCreations(); songSyncWorker.syncSongModifications(); songSyncWorker.syncSongDeletions(); + songSyncWorker.uploadNoteSheets(); return Result.success(); } catch (Exception e) { + e.printStackTrace(); return Result.failure(); } } diff --git a/app/src/main/java/core/notevault/sync/synchronisation/SyncService.java b/app/src/main/java/core/notevault/sync/synchronisation/SyncService.java index 068f95e..d63bda1 100644 --- a/app/src/main/java/core/notevault/sync/synchronisation/SyncService.java +++ b/app/src/main/java/core/notevault/sync/synchronisation/SyncService.java @@ -7,9 +7,14 @@ import core.notevault.sync.synchronisation.songs.deletion.SongDeletionBatchReque import core.notevault.sync.synchronisation.songs.deletion.SongDeletionBatchResponse; import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchRequest; import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchResponse; +import core.notevault.sync.synchronisation.songs.notesheets.UploadResponse; +import okhttp3.MultipartBody; +import okhttp3.RequestBody; import retrofit2.Call; import retrofit2.http.Body; +import retrofit2.http.Multipart; import retrofit2.http.POST; +import retrofit2.http.Part; public interface SyncService { @@ -21,4 +26,9 @@ public interface SyncService { @POST("/sync/songs/delete") Call performSongDeletion(@Body SongDeletionBatchRequest syncRequest); + + @Multipart + @POST("/sync/songs/note_sheet/upload") + Call uploadNoteSheet(@Part("serverID") RequestBody serverID, @Part("fileName") RequestBody fileName, + @Part MultipartBody.Part image); } diff --git a/app/src/main/java/core/notevault/sync/synchronisation/songs/SongSyncWorker.java b/app/src/main/java/core/notevault/sync/synchronisation/songs/SongSyncWorker.java index 3b9dd64..5ced875 100644 --- a/app/src/main/java/core/notevault/sync/synchronisation/songs/SongSyncWorker.java +++ b/app/src/main/java/core/notevault/sync/synchronisation/songs/SongSyncWorker.java @@ -5,6 +5,7 @@ import android.os.AsyncTask; import android.widget.Toast; import core.notevault.data.MusicNote; import core.notevault.data.MusicNoteDAO; +import core.notevault.data.NoteSheet; import core.notevault.data.sync.SyncStatus; import core.notevault.sync.synchronisation.SyncService; import core.notevault.sync.synchronisation.songs.creation.SongCreationBatchRequest; @@ -15,13 +16,19 @@ import core.notevault.sync.synchronisation.songs.deletion.SongDeletionBatchRespo import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchRequest; import core.notevault.sync.synchronisation.songs.modification.SongModificationBatchResponse; import core.notevault.sync.synchronisation.songs.modification.SongModificationRequest; +import core.notevault.sync.synchronisation.songs.notesheets.UploadResponse; +import okhttp3.MediaType; +import okhttp3.MultipartBody; +import okhttp3.RequestBody; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; +import java.io.File; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.stream.Collectors; @@ -118,4 +125,47 @@ public class SongSyncWorker { } }); } + + public void uploadNoteSheets() { + List noteSheets = database.getNoteSheetsWithStatus(SyncStatus.CREATED); + for(NoteSheet noteSheet : noteSheets) { + Optional serverID = database.getMusicNoteByLocalID(noteSheet.getMusicNoteId()); + + if(serverID.isPresent()) { + File imageFile = new File(noteSheet.getFilePath()); + // Erstelle Request-Bodies für serverID und fileName + RequestBody serverIDBody = RequestBody.create(MediaType.parse("text/plain"), serverID.get()); + RequestBody fileNameBody = RequestBody.create(MediaType.parse("text/plain"), imageFile.getName()); + + // Erstelle den Multipart-Body für die Bilddatei + RequestBody imageRequestBody = RequestBody.create(MediaType.parse("image/*"), imageFile); + MultipartBody.Part imagePart = MultipartBody.Part.createFormData("image", imageFile.getName(), imageRequestBody); + + syncService.uploadNoteSheet(serverIDBody, fileNameBody, imagePart).enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful() && response.body() != null) { + // Erfolg: Antwort verarbeiten + Toast.makeText(context, "Upload erfolgreich", Toast.LENGTH_LONG).show(); + executorService.execute(() -> { + noteSheet.setSyncStatus(SyncStatus.SYNCED); + database.updateNoteSheet(noteSheet); + }); + } else { + // Fehler: Antwort prüfen + Toast.makeText(context, "Fehler beim Upload: " + response.code(), Toast.LENGTH_LONG).show(); + } + + } + + @Override + public void onFailure(Call call, Throwable throwable) { + Toast.makeText(context, "Fehler beim Upload: " + throwable.toString(), Toast.LENGTH_LONG).show(); + } + }); + } + + + } + } } diff --git a/app/src/main/java/core/notevault/sync/synchronisation/songs/notesheets/UploadResponse.java b/app/src/main/java/core/notevault/sync/synchronisation/songs/notesheets/UploadResponse.java new file mode 100644 index 0000000..05a4801 --- /dev/null +++ b/app/src/main/java/core/notevault/sync/synchronisation/songs/notesheets/UploadResponse.java @@ -0,0 +1,5 @@ +package core.notevault.sync.synchronisation.songs.notesheets; + +public class UploadResponse { + private String serverID; +}