Skip to content

Commit

Permalink
Cleanup files after upload on native (#3597)
Browse files Browse the repository at this point in the history
* cleanup images after uploading from the composer

cleanup external embed images as well

cleanup files after upload

add `expo-file-system`

* remove test log

* await

* Revert "await"

This reverts commit 771814b.

* just log an error if it throws
  • Loading branch information
haileyok authored Apr 17, 2024
1 parent a2df814 commit 410f155
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
"expo-constants": "~15.4.5",
"expo-dev-client": "~3.3.8",
"expo-device": "~5.9.3",
"expo-file-system": "^16.0.9",
"expo-haptics": "^12.8.1",
"expo-image": "~1.10.6",
"expo-image-manipulator": "^11.8.0",
Expand Down
31 changes: 25 additions & 6 deletions src/lib/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {deleteAsync} from 'expo-file-system'
import {
AppBskyEmbedImages,
AppBskyEmbedExternal,
AppBskyEmbedImages,
AppBskyEmbedRecord,
AppBskyEmbedRecordWithMedia,
AppBskyFeedThreadgate,
Expand All @@ -11,13 +12,14 @@ import {
RichText,
} from '@atproto/api'
import {AtUri} from '@atproto/api'
import {isNetworkError} from 'lib/strings/errors'
import {LinkMeta} from '../link-meta/link-meta'
import {isWeb} from 'platform/detection'
import {ImageModel} from 'state/models/media/image'
import {shortenLinks} from 'lib/strings/rich-text-manip'

import {logger} from '#/logger'
import {ThreadgateSetting} from '#/state/queries/threadgate'
import {isNetworkError} from 'lib/strings/errors'
import {shortenLinks} from 'lib/strings/rich-text-manip'
import {isNative, isWeb} from 'platform/detection'
import {ImageModel} from 'state/models/media/image'
import {LinkMeta} from '../link-meta/link-meta'

export interface ExternalEmbedDraft {
uri: string
Expand Down Expand Up @@ -117,6 +119,15 @@ export async function post(agent: BskyAgent, opts: PostOpts) {
const {width, height} = image.compressed || image
logger.debug(`Uploading image`)
const res = await uploadBlob(agent, path, 'image/jpeg')

if (isNative) {
try {
deleteAsync(path)
} catch (e) {
console.error(e)
}
}

images.push({
image: res.data.blob,
alt: image.altText ?? '',
Expand Down Expand Up @@ -171,6 +182,14 @@ export async function post(agent: BskyAgent, opts: PostOpts) {
encoding,
)
thumb = thumbUploadRes.data.blob

try {
if (isNative) {
deleteAsync(opts.extLink.localThumb.path)
}
} catch (e) {
console.error(e)
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11852,6 +11852,11 @@ expo-eas-client@~0.11.0:
resolved "https://registry.yarnpkg.com/expo-eas-client/-/expo-eas-client-0.11.0.tgz#0f25aa497849cade7ebef55c0631093a87e58b07"
integrity sha512-99W0MUGe3U4/MY1E9UeJ4uKNI39mN8/sOGA0Le8XC47MTbwbLoVegHR3C5y2fXLwLn7EpfNxAn5nlxYjY3gD2A==

expo-file-system@^16.0.9:
version "16.0.9"
resolved "https://registry.yarnpkg.com/expo-file-system/-/expo-file-system-16.0.9.tgz#cbd6c4b228b60a6b6c71fd1b91fe57299fb24da7"
integrity sha512-3gRPvKVv7/Y7AdD9eHMIdfg5YbUn2zbwKofjsloTI5sEC57SLUFJtbLvUCz9Pk63DaSQ7WIE1JM0EASyvuPbuw==

expo-file-system@~16.0.0:
version "16.0.1"
resolved "https://registry.yarnpkg.com/expo-file-system/-/expo-file-system-16.0.1.tgz#326b7c2f6e53e1a0eaafc9769578aafb3f9c9f43"
Expand Down

0 comments on commit 410f155

Please sign in to comment.