Skip to content

Commit ccef747

Browse files
committed
Refactor
Part of #381
1 parent e10b6ef commit ccef747

6 files changed

Lines changed: 38 additions & 53 deletions

File tree

src/lib/components/assets/browser/select-assets-dialog.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import { normalize } from '$lib/services/search';
3030
import { isSmallScreen } from '$lib/services/user/env';
3131
import { prefs } from '$lib/services/user/prefs';
32+
import { getGitHash } from '$lib/services/utils/file';
3233
import { SUPPORTED_IMAGE_TYPES } from '$lib/services/utils/media/image';
3334
3435
/**
@@ -181,7 +182,7 @@
181182
blobURL: blobURL ?? URL.createObjectURL(file),
182183
name,
183184
path: `${targetFolderPath}/${name}`,
184-
sha: await getHash(file),
185+
sha: await getGitHash(file),
185186
size,
186187
kind: getAssetKind(name),
187188
folder,

src/lib/components/contents/details/widgets/file/file-editor.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import { getDefaultMediaLibraryOptions, transformFile } from '$lib/services/assets/media-library';
2727
import { entryDraft } from '$lib/services/contents/draft';
2828
import { hasMouse } from '$lib/services/user/env';
29-
import { createPath, formatSize } from '$lib/services/utils/file';
29+
import { createPath, formatSize, getGitHash } from '$lib/services/utils/file';
3030
import { SUPPORTED_IMAGE_TYPES } from '$lib/services/utils/media/image';
3131
3232
/**
@@ -182,9 +182,9 @@
182182
file = await transformFile(file, transformations);
183183
}
184184
185-
const hash = await getHash(file);
185+
const sha = await getGitHash(file);
186186
const { folder } = selectedResource;
187-
const existingAsset = $allAssets.find((a) => a.sha === hash && equal(a.folder, folder));
187+
const existingAsset = $allAssets.find((a) => a.sha === sha && equal(a.folder, folder));
188188
189189
if (existingAsset) {
190190
// If the selected file has already been uploaded, use the existing asset instead of

src/lib/services/assets/data/create.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { getHash } from '@sveltia/utils/crypto';
21
import { get } from 'svelte/store';
32
import {
43
allAssets,
@@ -12,7 +11,7 @@ import { getDefaultMediaLibraryOptions } from '$lib/services/assets/media-librar
1211
import { backend } from '$lib/services/backends';
1312
import { siteConfig } from '$lib/services/config';
1413
import { UPDATE_TOAST_DEFAULT_STATE } from '$lib/services/contents/collection/data';
15-
import { formatFileName } from '$lib/services/utils/file';
14+
import { formatFileName, getGitHash } from '$lib/services/utils/file';
1615

1716
/**
1817
* @import { Asset, CommitAction, CommitChangesOptions, UploadingAssets } from '$lib/types/private';
@@ -110,7 +109,7 @@ export const saveAssets = async (uploadingAssets, options) => {
110109
blobURL: URL.createObjectURL(file),
111110
name,
112111
path,
113-
sha: await getHash(file),
112+
sha: await getGitHash(file),
114113
size: file.size,
115114
kind: getAssetKind(name),
116115
text: undefined,

src/lib/services/assets/data/move.js

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,29 @@ import { getAssociatedCollections } from '$lib/services/contents/entry';
3737
* Update the asset and entry stores after moving or renaming assets.
3838
* @param {object} args Arguments.
3939
* @param {'move' | 'rename'} args.action The action performed, either 'move' or 'rename'.
40-
* @param {MovingAsset[]} args.movingAssets The list of assets being moved or renamed.
41-
* @param {Asset[]} args.savingAssets The updated asset objects to be saved in the store.
42-
* @param {Entry[]} args.savingEntries The updated entry objects to be saved in the store.
40+
* @param {MovingAsset[]} args.movedAssets The assets that have been moved or renamed.
41+
* @param {Asset[]} args.savedAssets The assets that have been saved.
42+
* @param {Entry[]} args.savedEntries The entries that have been saved.
4343
*/
44-
const updateStores = ({ action, movingAssets, savingAssets, savingEntries }) => {
45-
const savingAssetsPaths = movingAssets.map((a) => a.asset.path); // old paths
46-
const savingEntryIds = savingEntries.map((e) => e.id);
44+
const updateStores = ({ action, movedAssets, savedAssets, savedEntries }) => {
45+
const savedAssetsPaths = movedAssets.map((a) => a.asset.path); // old paths
46+
const savedEntryIds = savedEntries.map((e) => e.id);
4747

4848
allAssets.update((assets) => [
49-
...assets.filter((a) => !savingAssetsPaths.includes(a.path)),
50-
...savingAssets,
49+
...assets.filter((a) => !savedAssetsPaths.includes(a.path)),
50+
...savedAssets,
5151
]);
5252

5353
allEntries.update((entries) => [
54-
...entries.filter((e) => !savingEntryIds.includes(e.id)),
55-
...savingEntries,
54+
...entries.filter((e) => !savedEntryIds.includes(e.id)),
55+
...savedEntries,
5656
]);
5757

5858
const _allAssets = get(allAssets);
5959
const focusedAssetPath = get(focusedAsset)?.path;
60-
const _focusedAsset = movingAssets.find((a) => a.asset.path === focusedAssetPath);
60+
const _focusedAsset = movedAssets.find((a) => a.asset.path === focusedAssetPath);
6161
const overlaidAssetPath = get(overlaidAsset)?.path;
62-
const _overlaidAsset = movingAssets.find((a) => a.asset.path === overlaidAssetPath);
62+
const _overlaidAsset = movedAssets.find((a) => a.asset.path === overlaidAssetPath);
6363

6464
// Replace the existing asset
6565
if (_focusedAsset) {
@@ -75,7 +75,7 @@ const updateStores = ({ action, movingAssets, savingAssets, savingEntries }) =>
7575
...UPDATE_TOAST_DEFAULT_STATE,
7676
moved: action === 'move',
7777
renamed: action === 'rename',
78-
count: movingAssets.length,
78+
count: movedAssets.length,
7979
});
8080
};
8181

@@ -214,14 +214,15 @@ export const moveAssets = async (action, movingAssets) => {
214214

215215
const results = await get(backend)?.commitChanges(changes, { commitType: 'uploadMedia' });
216216

217-
// Update blob URLs for the local backend
218-
if (Array.isArray(results)) {
219-
savingAssets.forEach((asset, index) => {
220-
if (results[index] instanceof File) {
221-
asset.blobURL = URL.createObjectURL(/** @type {File} */ (results[index]));
222-
}
223-
});
224-
}
217+
updateStores({
218+
action,
219+
movedAssets: [...movingAssets],
220+
savedAssets: savingAssets.map((asset, index) => {
221+
const result = results?.[index];
222+
const blobURL = result instanceof File ? URL.createObjectURL(result) : undefined;
225223

226-
updateStores({ action, movingAssets, savingAssets, savingEntries });
224+
return { ...asset, blobURL };
225+
}),
226+
savedEntries: [...savingEntries],
227+
});
227228
};

src/lib/services/backends/shared/fs.js

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
/* eslint-disable no-restricted-syntax */
44

55
import { unique } from '@sveltia/utils/array';
6-
import { getHash } from '@sveltia/utils/crypto';
76
import { getPathInfo, readAsText } from '@sveltia/utils/file';
87
import { stripSlashes } from '@sveltia/utils/string';
98
import { get } from 'svelte/store';
@@ -13,7 +12,7 @@ import { GIT_CONFIG_FILE_REGEX, gitConfigFiles } from '$lib/services/backends';
1312
import { createFileList } from '$lib/services/backends/shared/fetch';
1413
import { allEntries, allEntryFolders, dataLoaded, entryParseErrors } from '$lib/services/contents';
1514
import { prepareEntries } from '$lib/services/contents/file/process';
16-
import { createPathRegEx } from '$lib/services/utils/file';
15+
import { createPathRegEx, getGitHash } from '$lib/services/utils/file';
1716

1817
/**
1918
* @import { BaseFileListItem, BaseFileListItemProps, FileChange } from '$lib/types/private';
@@ -146,23 +145,6 @@ const scanDir = async (dirHandle, context) => {
146145
}
147146
};
148147

149-
/**
150-
* Asynchronously computes and returns the hash of a given file.
151-
* @param {File} file The file object to compute the hash for.
152-
* @returns {Promise<string>} The computed hash as a string, or an empty string if an error occurs.
153-
*/
154-
const getFileHash = async (file) => {
155-
try {
156-
// Need `await` here to catch any exception
157-
return await getHash(file);
158-
} catch (/** @type {any} */ ex) {
159-
// eslint-disable-next-line no-console
160-
console.error(ex);
161-
}
162-
163-
return '';
164-
};
165-
166148
/**
167149
* Normalize a file list item to ensure it has the required properties. This function also computes
168150
* the SHA-1 hash of the file. The file path and name must be normalized, as certain non-ASCII
@@ -175,7 +157,7 @@ const normalizeFileListItem = async ({ file, path }) => ({
175157
path: path.normalize(),
176158
name: file.name.normalize(),
177159
size: file.size,
178-
sha: await getFileHash(file),
160+
sha: await getGitHash(file),
179161
});
180162

181163
/**

src/lib/types/private.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@
243243
/**
244244
* Base file info retrieved from a Git repository.
245245
* @typedef {object} RepositoryFileInfo
246-
* @property {string} sha SHA-1 hash for the file.
246+
* @property {string} sha Git object ID (SHA-1 hash) for the file.
247247
* @property {number} size File size in bytes.
248248
* @property {string} [text] Raw text for a plaintext file, like HTML or Markdown.
249249
* @property {RepositoryFileMetadata} meta Metadata from the repository.
@@ -278,7 +278,7 @@
278278
* @property {File} [file] File object. Local backend only.
279279
* @property {string} path File path.
280280
* @property {string} name File name, without a path.
281-
* @property {string} sha SHA-1 hash for the file.
281+
* @property {string} sha Git object ID (SHA-1 hash) for the file.
282282
* @property {number} size File size in bytes.
283283
* @property {string} [text] Raw text for a plaintext file, like HTML or Markdown.
284284
* @property {RepositoryFileMetadata} [meta] Metadata from the repository. Git backends only.
@@ -574,7 +574,9 @@
574574
* @property {string} [previousPath] Original path to a file being moved. Required when the commit
575575
* `action` is `move`.
576576
* @property {string} [slug] Entry slug or `undefined` for an asset.
577-
* @property {string | File} [data] File data.
577+
* @property {string | File} [data] File data. `undefined` for a deleted file, or a file object for
578+
* a new or updated file. It can also be a string for a text file like Markdown or HTML, which is
579+
* automatically converted to a Blob.
578580
*/
579581

580582
/**
@@ -612,7 +614,7 @@
612614
* fetched or a local file being uploaded. Or `undefined` if the URL is not generated yet.
613615
* @property {string} name File name.
614616
* @property {string} path File path.
615-
* @property {string} sha SHA-1 hash for the file.
617+
* @property {string} sha Git object ID (SHA-1 hash) for the file.
616618
* @property {number} size File size in bytes.
617619
* @property {AssetKind} kind Basic file type.
618620
* @property {string} [text] Raw text for a plaintext file, like HTML or Markdown.

0 commit comments

Comments
 (0)