-
-
Notifications
You must be signed in to change notification settings - Fork 44
Add Quillpad import and fix Notally import #978
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
app/src/main/java/com/philkes/notallyx/data/imports/quillpad/QuillpadBackup.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package com.philkes.notallyx.data.imports.quillpad | ||
|
|
||
| import kotlinx.serialization.Serializable | ||
|
|
||
| @Serializable | ||
| data class QuillpadBackup( | ||
| val notes: List<QuillpadNote> = emptyList(), | ||
| val notebooks: List<QuillpadNotebook> = emptyList(), | ||
| val tags: List<QuillpadTag> = emptyList(), | ||
| val reminders: List<QuillpadReminder> = emptyList(), | ||
| val joins: List<QuillpadJoin> = emptyList(), | ||
| ) | ||
|
|
||
| @Serializable | ||
| data class QuillpadNote( | ||
| val id: Long, | ||
| val title: String? = null, | ||
| val content: String? = null, | ||
| val isList: Boolean = false, | ||
| val taskList: List<QuillpadTask>? = null, | ||
| val isPinned: Boolean = false, | ||
| val isArchived: Boolean = false, | ||
| val isDeleted: Boolean = false, | ||
| val creationDate: Long, | ||
| val modifiedDate: Long, | ||
| val notebookId: Long? = null, | ||
| val tags: List<QuillpadTag>? = null, | ||
| val attachments: List<QuillpadAttachment>? = null, | ||
| val reminders: List<QuillpadReminder> = emptyList(), | ||
| ) | ||
|
|
||
| @Serializable data class QuillpadTask(val id: Int, val content: String, val isDone: Boolean) | ||
|
|
||
| @Serializable data class QuillpadNotebook(val id: Long, val name: String) | ||
|
|
||
| @Serializable data class QuillpadTag(val id: Long, val name: String) | ||
|
|
||
| @Serializable | ||
| data class QuillpadReminder( | ||
| val id: Long, | ||
| val noteId: Long, | ||
| val date: Long, | ||
| val name: String? = null, | ||
| ) | ||
|
|
||
| @Serializable data class QuillpadJoin(val tagId: Long, val noteId: Long) | ||
|
|
||
| @Serializable | ||
| data class QuillpadAttachment( | ||
| val type: String? = null, | ||
| val description: String? = null, | ||
| val fileName: String, | ||
| ) |
229 changes: 229 additions & 0 deletions
229
app/src/main/java/com/philkes/notallyx/data/imports/quillpad/QuillpadImporter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| package com.philkes.notallyx.data.imports.quillpad | ||
|
|
||
| import android.app.Application | ||
| import android.net.Uri | ||
| import androidx.lifecycle.MutableLiveData | ||
| import com.philkes.notallyx.R | ||
| import com.philkes.notallyx.data.imports.ExternalImporter | ||
| import com.philkes.notallyx.data.imports.ImportException | ||
| import com.philkes.notallyx.data.imports.ImportProgress | ||
| import com.philkes.notallyx.data.imports.ImportStage | ||
| import com.philkes.notallyx.data.imports.markdown.parseBodyAndSpansFromMarkdown | ||
| import com.philkes.notallyx.data.model.Audio | ||
| import com.philkes.notallyx.data.model.BaseNote | ||
| import com.philkes.notallyx.data.model.FileAttachment | ||
| import com.philkes.notallyx.data.model.Folder | ||
| import com.philkes.notallyx.data.model.ListItem | ||
| import com.philkes.notallyx.data.model.NoteViewMode | ||
| import com.philkes.notallyx.data.model.Reminder | ||
| import com.philkes.notallyx.data.model.Type | ||
| import com.philkes.notallyx.utils.getMimeType | ||
| import com.philkes.notallyx.utils.moveAllFiles | ||
| import com.philkes.notallyx.utils.toMillis | ||
| import java.io.File | ||
| import java.io.FileOutputStream | ||
| import java.io.IOException | ||
| import java.io.InputStream | ||
| import java.util.Date | ||
| import java.util.zip.ZipEntry | ||
| import java.util.zip.ZipInputStream | ||
| import kotlin.collections.forEach | ||
| import kotlin.collections.map | ||
| import kotlinx.serialization.ExperimentalSerializationApi | ||
| import kotlinx.serialization.json.Json | ||
|
|
||
| @OptIn(ExperimentalSerializationApi::class) | ||
| class QuillpadImporter : ExternalImporter { | ||
|
|
||
| internal val json = Json { | ||
| ignoreUnknownKeys = true | ||
| isLenient = true | ||
| allowTrailingComma = true | ||
| } | ||
|
|
||
| override fun import( | ||
| app: Application, | ||
| source: Uri, | ||
| destination: File, | ||
| progress: MutableLiveData<ImportProgress>?, | ||
| ): Pair<List<BaseNote>, File> { | ||
| progress?.postValue(ImportProgress(indeterminate = true, stage = ImportStage.EXTRACT_FILES)) | ||
| val dataFolder = | ||
| try { | ||
| app.contentResolver.openInputStream(source)!!.use { unzip(destination, it) } | ||
| } catch (e: Exception) { | ||
| throw ImportException(R.string.invalid_quillpad, e) | ||
| } | ||
|
|
||
| val mediaFolder = File(dataFolder, "media") | ||
| if (mediaFolder.exists() && mediaFolder.isDirectory) { | ||
| mediaFolder.moveAllFiles(dataFolder) | ||
| } | ||
|
|
||
| val backupFile = File(dataFolder, "backup.json") | ||
| if (!backupFile.exists()) { | ||
| throw ImportException( | ||
| R.string.invalid_quillpad, | ||
| RuntimeException("backup.json not found in ZIP"), | ||
| ) | ||
| } | ||
|
|
||
| val quillpadBackup = | ||
| try { | ||
| json.decodeFromString<QuillpadBackup>(backupFile.readText()) | ||
| } catch (e: Exception) { | ||
| throw ImportException(R.string.invalid_quillpad, e) | ||
| } | ||
|
|
||
| val notebookMap = quillpadBackup.notebooks.associate { it.id to it.name } | ||
| val total = quillpadBackup.notes.size | ||
| progress?.postValue(ImportProgress(0, total, stage = ImportStage.IMPORT_NOTES)) | ||
| var counter = 1 | ||
|
|
||
| val baseNotes = | ||
| quillpadBackup.notes.map { quillpadNote -> | ||
| val result = quillpadNote.toBaseNote(notebookMap) | ||
| progress?.postValue( | ||
| ImportProgress(counter++, total, stage = ImportStage.IMPORT_NOTES) | ||
| ) | ||
| result | ||
| } | ||
|
|
||
| return Pair(baseNotes, dataFolder) | ||
| } | ||
|
|
||
| fun QuillpadNote.toBaseNote(notebookMap: Map<Long, String>): BaseNote { | ||
| val (body, spans) = | ||
| if (!isList && content != null) { | ||
| parseBodyAndSpansFromMarkdown(content) | ||
| } else { | ||
| Pair("", emptyList()) | ||
| } | ||
|
|
||
| val items = | ||
| taskList?.mapIndexed { index, task -> | ||
| ListItem( | ||
| body = task.content, | ||
| checked = task.isDone, | ||
| isChild = false, | ||
| order = index, | ||
| children = mutableListOf(), | ||
| ) | ||
| } ?: emptyList() | ||
|
|
||
| val images = mutableListOf<FileAttachment>() | ||
| val files = mutableListOf<FileAttachment>() | ||
| val audios = mutableListOf<Audio>() | ||
|
|
||
| attachments?.forEach { attachment -> | ||
| when (attachment.type) { | ||
| "AUDIO" -> | ||
| audios.add( | ||
| Audio( | ||
| name = attachment.fileName, | ||
| duration = null, | ||
| timestamp = modifiedDate.toMillis(), | ||
| ) | ||
| ) | ||
| else -> { | ||
| val mimetype = attachment.fileName.getMimeType() | ||
| if (mimetype?.startsWith("image/") == true) { | ||
| images.add( | ||
| FileAttachment( | ||
| localName = attachment.fileName, | ||
| originalName = attachment.description ?: attachment.fileName, | ||
| mimeType = mimetype, | ||
| ) | ||
| ) | ||
| } else { | ||
| files.add( | ||
| FileAttachment( | ||
| localName = attachment.fileName, | ||
| originalName = attachment.description ?: attachment.fileName, | ||
| mimeType = mimetype ?: "application/octet-stream", | ||
| ) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val labels = mutableSetOf<String>() | ||
| notebookId?.let { notebookId -> notebookMap[notebookId]?.let { labels.add(it) } } | ||
| tags?.forEach { labels.add(it.name) } | ||
|
|
||
| val reminders = | ||
| this.reminders.map { Reminder(id = it.id, dateTime = Date(it.date), repetition = null) } | ||
|
|
||
| return BaseNote( | ||
| id = 0L, | ||
| type = if (isList) Type.LIST else Type.NOTE, | ||
| folder = | ||
| when { | ||
| isDeleted -> Folder.DELETED | ||
| isArchived -> Folder.ARCHIVED | ||
| else -> Folder.NOTES | ||
| }, | ||
| color = BaseNote.COLOR_DEFAULT, | ||
| title = title ?: "", | ||
| pinned = isPinned, | ||
| timestamp = creationDate.toMillis(), | ||
| modifiedTimestamp = modifiedDate.toMillis(), | ||
| labels = labels.sorted().toList(), | ||
| body = body, | ||
| spans = spans, | ||
| items = items, | ||
| images = images, | ||
| files = files, | ||
| audios = audios, | ||
| reminders = reminders, | ||
| viewMode = NoteViewMode.EDIT, | ||
| isPinnedToStatus = false, | ||
| ) | ||
| } | ||
|
|
||
| private fun unzip(destinationPath: File, inputStream: InputStream): File { | ||
| val buffer = ByteArray(1024) | ||
| val zis = ZipInputStream(inputStream) | ||
| var zipEntry = zis.nextEntry | ||
| while (zipEntry != null) { | ||
| val newFile = newFile(destinationPath, zipEntry) | ||
| if (zipEntry.isDirectory) { | ||
| if (!newFile.isDirectory && !newFile.mkdirs()) { | ||
| throw IOException("Failed to create directory $newFile") | ||
| } | ||
| } else { | ||
| val parent = newFile.parentFile | ||
| if (parent != null) { | ||
| if (!parent.isDirectory && !parent.mkdirs()) { | ||
| throw IOException("Failed to create directory $parent") | ||
| } | ||
| } | ||
| FileOutputStream(newFile).use { | ||
| var len: Int | ||
| while ((zis.read(buffer).also { length -> len = length }) > 0) { | ||
| it.write(buffer, 0, len) | ||
| } | ||
| } | ||
| } | ||
| zipEntry = zis.nextEntry | ||
| } | ||
| zis.closeEntry() | ||
| zis.close() | ||
| return destinationPath | ||
|
Crustack marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private fun newFile(destinationDir: File, zipEntry: ZipEntry): File { | ||
| val destFile = File(destinationDir, zipEntry.name) | ||
| val destDirPath = destinationDir.canonicalPath | ||
| val destFilePath = destFile.canonicalPath | ||
| if (!destFilePath.startsWith(destDirPath + File.separator)) { | ||
| throw IOException("Entry is outside of the target dir: " + zipEntry.name) | ||
| } | ||
| return destFile | ||
| } | ||
|
|
||
| companion object { | ||
| private const val TAG = "QuillpadImporter" | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.