Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.json.JSONObject
abstract class BaseSingleObjectSyncAdapter<T>(
protected var db: SyncDatabase,
protected var eventSlug: String,
protected var fileStorage: FileStorage,
protected var key: String,
protected var api: PretixApi,
protected var syncCycleId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import org.json.JSONObject

class EventSyncAdapter(
db: SyncDatabase,
fileStorage: FileStorage,
eventSlug: String,
key: String,
api: PretixApi,
syncCycleId: String,
feedback: ProgressFeedback? = null,
) : BaseSingleObjectSyncAdapter<Event>(
db = db,
fileStorage = fileStorage,
eventSlug = eventSlug,
key = key,
api = api,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import eu.pretix.libpretixsync.sync.SyncManager.ProgressFeedback

class InvoiceSettingsSyncAdapter(
db: SyncDatabase,
fileStorage: FileStorage,
eventSlug: String,
key: String,
api: PretixApi,
syncCycleId: String,
feedback: ProgressFeedback? = null,
) : SettingsSyncAdapter(
db = db,
fileStorage = fileStorage,
eventSlug = eventSlug,
key = key,
api = api,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class OrderCleanup(val db: SyncDatabase, val fileStorage: FileStorage, val api:
return subeventsDeletionDate[sid]
}
try {
SubEventSyncAdapter(db, eventSlug, sid.toString(), api, syncCycleId) { }.download()
SubEventSyncAdapter(db, fileStorage, eventSlug, sid.toString(), api, syncCycleId) { }.download()
} catch (e: JSONException) {
subeventsDeletionDate[sid] = null
return null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package eu.pretix.libpretixsync.sync

import app.cash.sqldelight.TransactionWithoutReturn
import eu.pretix.libpretixsync.api.ApiException
import eu.pretix.libpretixsync.api.PretixApi
import eu.pretix.libpretixsync.sqldelight.Settings
import eu.pretix.libpretixsync.sqldelight.SyncDatabase
import eu.pretix.libpretixsync.sync.SyncManager.ProgressFeedback
import eu.pretix.libpretixsync.utils.HashUtils
import org.json.JSONObject
import java.io.IOException
import java.io.OutputStream

open class SettingsSyncAdapter(
db: SyncDatabase,
fileStorage: FileStorage,
eventSlug: String,
key: String,
api: PretixApi,
syncCycleId: String,
feedback: ProgressFeedback? = null,
) : BaseSingleObjectSyncAdapter<Settings>(
db = db,
fileStorage = fileStorage,
eventSlug = eventSlug,
key = key,
api = api,
Expand Down Expand Up @@ -43,6 +49,11 @@ open class SettingsSyncAdapter(
override fun getJSON(obj: Settings): JSONObject = JSONObject(obj.json_data!!)

override fun insert(jsonobj: JSONObject) {
var jsonobj = jsonobj
listOf("pretixkiosk_screensaver_image").forEach { fieldName ->
jsonobj = processAndUpdateJSONdataWithPicture(jsonobj, fieldName, null)
}

db.settingsQueries.insert(
slug = eventSlug,
address = jsonobj.optString("invoice_address_from"),
Expand All @@ -58,6 +69,12 @@ open class SettingsSyncAdapter(
}

override fun update(obj: Settings, jsonobj: JSONObject) {
var jsonobj = jsonobj
val objjsondata = JSONObject(obj.json_data)
listOf("pretixkiosk_screensaver_image").forEach { fieldName ->
jsonobj = processAndUpdateJSONdataWithPicture(jsonobj, fieldName, if (objjsondata.isNull(fieldName)) null else objjsondata.getString(fieldName))
}

db.settingsQueries.updateFromJson(
address = jsonobj.optString("invoice_address_from"),
city = jsonobj.optString("invoice_address_from_city"),
Expand All @@ -75,4 +92,69 @@ open class SettingsSyncAdapter(
override fun runInTransaction(body: TransactionWithoutReturn.() -> Unit) {
db.settingsQueries.transaction(false, body)
}

private fun processAndUpdateJSONdataWithPicture(jsonobj: JSONObject, fieldName: String, oldFilename: String?): JSONObject {
if (jsonobj.has(fieldName)) {
val pictureFilename = processPicture(jsonobj, fieldName, oldFilename)

jsonobj.put(fieldName, pictureFilename)
}

return jsonobj
}

private fun processPicture(jsonobj: JSONObject, fieldName: String, oldFilename: String?): String? {
val remote_filename: String = jsonobj.optString(fieldName)
var result: String? = null;

if (remote_filename.startsWith("http")) {
val hash = HashUtils.toSHA1(remote_filename.toByteArray())
val local_filename =
"settings_" + fieldName + "_" + hash + remote_filename.substring(
remote_filename.lastIndexOf(".")
)
if (oldFilename != null && oldFilename != local_filename) {
fileStorage.delete(oldFilename)
result = null
}
if (!fileStorage.contains(local_filename)) {
try {
val file = api.downloadFile(remote_filename)

val outStream: OutputStream = fileStorage.writeStream(local_filename)
val inStream = file.response.body?.byteStream()

if (inStream == null) {
outStream.close()
throw IOException()
}

val buffer = ByteArray(1444)
var byteread: Int
while (inStream.read(buffer).also { byteread = it } != -1) {
outStream.write(buffer, 0, byteread)
}
inStream.close()
outStream.close()
result = local_filename
} catch (e: ApiException) {
// TODO: What to do?
e.printStackTrace()
} catch (e: IOException) {
// TODO: What to do?
e.printStackTrace()
fileStorage.delete(local_filename)
}
} else {
result = local_filename
}
} else {
if (oldFilename != null) {
fileStorage.delete(oldFilename)
result = null
}
}

return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import org.json.JSONObject

class SubEventSyncAdapter(
db: SyncDatabase,
fileStorage: FileStorage,
eventSlug: String,
key: String,
api: PretixApi,
syncCycleId: String,
feedback: ProgressFeedback? = null,
) : BaseSingleObjectSyncAdapter<SubEvent>(
db = db,
fileStorage = fileStorage,
eventSlug = eventSlug,
key = key,
api = api,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ protected void downloadData(ProgressFeedback feedback, Boolean skip_orders, Stri
subEvent = overrideSubeventId;
}
try {
download(new EventSyncAdapter(db, eventSlug, eventSlug, api, configStore.getSyncCycleId(), feedback));
download(new EventSyncAdapter(db, fileStorage, eventSlug, eventSlug, api, configStore.getSyncCycleId(), feedback));
} catch (PermissionDeniedApiException e) {
e.eventSlug = eventSlug;
throw e;
Expand Down Expand Up @@ -444,12 +444,12 @@ protected void downloadData(ProgressFeedback feedback, Boolean skip_orders, Stri
}

try {
download(new SettingsSyncAdapter(db, eventSlug, eventSlug, api, configStore.getSyncCycleId(), feedback));
download(new SettingsSyncAdapter(db, fileStorage, eventSlug, eventSlug, api, configStore.getSyncCycleId(), feedback));
} catch (ApiException e) {
// Older pretix installations
// We don't need these on pretixSCAN, so we can save some traffic
if (profile == Profile.PRETIXPOS) {
download(new InvoiceSettingsSyncAdapter(db, eventSlug, eventSlug, api, configStore.getSyncCycleId(), feedback));
download(new InvoiceSettingsSyncAdapter(db, fileStorage, eventSlug, eventSlug, api, configStore.getSyncCycleId(), feedback));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class AsyncCheckProviderTest : BaseDatabaseTest() {
fakeApi = FakePretixApi()
p = AsyncCheckProvider(configStore!!, db)

EventSyncAdapter(db, "demo", "demo", fakeApi!!, "", null).standaloneRefreshFromJSON(jsonResource("events/event1.json"))
EventSyncAdapter(db, "demo2", "demo2", fakeApi!!, "", null).standaloneRefreshFromJSON(jsonResource("events/event2.json"))
EventSyncAdapter(db, FakeFileStorage(), "demo", "demo", fakeApi!!, "", null).standaloneRefreshFromJSON(jsonResource("events/event1.json"))
EventSyncAdapter(db, FakeFileStorage(), "demo2", "demo2", fakeApi!!, "", null).standaloneRefreshFromJSON(jsonResource("events/event2.json"))
ItemSyncAdapter(db, FakeFileStorage(), "demo", fakeApi!!, "", null).standaloneRefreshFromJSON(jsonResource("items/item1.json"))
ItemSyncAdapter(db, FakeFileStorage(), "demo", fakeApi!!, "", null).standaloneRefreshFromJSON(jsonResource("items/item2.json"))
ItemSyncAdapter(db, FakeFileStorage(), "demo", fakeApi!!, "", null).standaloneRefreshFromJSON(jsonResource("items/item3.json"))
Expand Down Expand Up @@ -64,7 +64,7 @@ class AsyncCheckProviderTest : BaseDatabaseTest() {
CheckInListSyncAdapter(db, FakeFileStorage(), "demo2", fakeApi!!, "", null, 0).standaloneRefreshFromJSON(
jsonResource("checkinlists/event2-list7.json")
)
SubEventSyncAdapter(db, "demo", "14", fakeApi!!, "", null).standaloneRefreshFromJSON(jsonResource("subevents/subevent1.json"))
SubEventSyncAdapter(db, FakeFileStorage(), "demo", "14", fakeApi!!, "", null).standaloneRefreshFromJSON(jsonResource("subevents/subevent1.json"))

val osa = OrderSyncAdapter(db, FakeFileStorage(), "demo", 0, true, false, fakeApi!!, "", null)
osa.standaloneRefreshFromJSON(jsonResource("orders/order1.json"))
Expand Down
Loading