Skip to content
Merged
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 @@ -37,7 +37,7 @@ data class Project(
}

val isUploading
get() = collections.firstOrNull { it.isUploading } != null
get() = collections.any { it.isUploading }

val collections: List<Collection>
get() = find(Collection::class.java, "project_id = ?", id.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ abstract class Conduit(

open fun cancel() {
mCancelled = true
mMedia.save()
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,33 @@ class IaConduit(media: Media, context: Context) : Conduit(media, context) {

val client = SaveClient.get(mContext)

// TODO this should make sure we aren't accidentally using one of archive.org's metadata fields by accident
val slug = getSlug(mMedia.title)
val fileName = getUploadFileName(mMedia, true)
val metaJson = gson.toJson(mMedia)
val proof = getProof()

var basePath = "$slug-${Util.RandomString(4).nextString()}"
val url = "$ARCHIVE_API_ENDPOINT/$basePath/$fileName"
if (mMedia.serverUrl.isBlank()) {
// TODO this should make sure we aren't accidentally using one of archive.org's metadata fields by accident
val slug = getSlug(mMedia.title)
val newIdentifier = "$slug-${Util.RandomString(4).nextString()}"
// create an identifier for the upload
mMedia.serverUrl = newIdentifier
}

// upload content synchronously for progress
client.uploadContent(url, mimeType)
client.uploadContent(fileName, mimeType)

// upload metadata and proofs async, and report failures
basePath = "$slug-${Util.RandomString(4).nextString()}"
client.uploadMetaData(metaJson, basePath, fileName)
client.uploadMetaData(metaJson, fileName)

/// Upload ProofMode metadata, if enabled and successfully created.
for (file in proof) {
client.uploadProofFiles(file, basePath)
client.uploadProofFiles(file)
}

val finalPath = ARCHIVE_DETAILS_ENDPOINT + basePath
mMedia.serverUrl = finalPath

jobSucceeded()

return true
} catch (e: Exception) {
} catch (e: Throwable) {
jobFailed(e)
}

Expand All @@ -79,8 +78,11 @@ class IaConduit(media: Media, context: Context) : Conduit(media, context) {
// Ignored. Not used here.
}

private suspend fun OkHttpClient.uploadContent(url: String, mimeType: String) {
private suspend fun OkHttpClient.uploadContent(fileName: String, mimeType: String) {
val mediaUri = mMedia.originalFilePath

val url = "${ARCHIVE_API_ENDPOINT}/${mMedia.serverUrl}/$fileName"

val requestBody = RequestBodyUtil.create(
mContext.contentResolver,
Uri.parse(mediaUri),
Expand All @@ -101,15 +103,15 @@ class IaConduit(media: Media, context: Context) : Conduit(media, context) {
}

@Throws(IOException::class)
private fun OkHttpClient.uploadMetaData(content: String, basePath: String, fileName: String) {
private fun OkHttpClient.uploadMetaData(content: String, fileName: String) {
val requestBody = RequestBodyUtil.create(
textMediaType,
content.byteInputStream(),
content.length.toLong(),
createListener(cancellable = { !mCancelled })
)

val url = "$ARCHIVE_API_ENDPOINT/$basePath/$fileName.meta.json"
val url = "${ARCHIVE_API_ENDPOINT}/${mMedia.serverUrl}/$fileName.meta.json"

val request = Request.Builder()
.url(url)
Expand All @@ -122,15 +124,15 @@ class IaConduit(media: Media, context: Context) : Conduit(media, context) {

/// upload proof mode
@Throws(IOException::class)
private fun OkHttpClient.uploadProofFiles(uploadFile: File, basePath: String) {
private fun OkHttpClient.uploadProofFiles(uploadFile: File) {
val requestBody = RequestBodyUtil.create(
mContext.contentResolver,
Uri.fromFile(uploadFile),
uploadFile.length(),
textMediaType, createListener(cancellable = { !mCancelled })
)

val url = "$ARCHIVE_API_ENDPOINT/$basePath/${uploadFile.name}"
val url = "$ARCHIVE_API_ENDPOINT/${mMedia.serverUrl}/${uploadFile.name}"

val request = Request.Builder()
.url(url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class UploadManagerActivity : BaseActivity() {
}

private val mMessageReceiver: BroadcastReceiver = object : BroadcastReceiver() {
private val handler = Handler(Looper.getMainLooper())

override fun onReceive(context: Context, intent: Intent) {
val action = BroadcastManager.getAction(intent)
val mediaId = action?.mediaId ?: return
Expand All @@ -56,10 +58,10 @@ class UploadManagerActivity : BaseActivity() {
val media = Media.get(mediaId)

if (action == BroadcastManager.Action.Delete || media?.sStatus == Media.Status.Uploaded) {
mFrag?.removeItem(mediaId)
handler.post { mFrag?.removeItem(mediaId) }
}
else {
mFrag?.updateItem(mediaId)
handler.post { mFrag?.updateItem(mediaId) }
}

if (media?.sStatus == Media.Status.Error) {
Expand All @@ -70,7 +72,7 @@ class UploadManagerActivity : BaseActivity() {
}
}

Handler(Looper.getMainLooper()).post {
handler.post {
updateTitle()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.work.Configuration
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import net.opendasharchive.openarchive.CleanInsightsManager
import net.opendasharchive.openarchive.R
Expand Down Expand Up @@ -90,7 +91,7 @@ class UploadService : JobService() {
mKeepUploading = false
for (conduit in mConduits) conduit.cancel()
mConduits.clear()

scope.cancel()
return true
}

Expand Down