sanity-plugin-smart-asset-manager 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,29 @@
1
+ export interface Asset {
2
+ _id: string
3
+ _ref?: string
4
+ _type: string
5
+ url: string
6
+ extension: string
7
+ mimeType?: string
8
+ size: number
9
+ originalFilename?: string
10
+ metadata?: {
11
+ dimensions?: {
12
+ width: number
13
+ height: number
14
+ }
15
+ }
16
+ }
17
+
18
+ export interface ReferencedDocument {
19
+ _id: string
20
+ _type: string
21
+ title: string
22
+ }
23
+
24
+ export type UsageInfo = {[assetId: string]: ReferencedDocument[]}
25
+
26
+ export type AssetTab = 'all' | 'analysis' | 'unused'
27
+ export type SortOrder = '_createdAt' | 'size' | 'originalFilename'
28
+ export type AssetTypeFilter = 'all' | 'image' | 'file' | 'video' | 'audio'
29
+ export type SizeFilter = 'all' | 'small' | 'medium' | 'large'
@@ -0,0 +1,25 @@
1
+ import {describe, it, expect} from 'vitest'
2
+ import {formatBytes} from '../formatBytes'
3
+
4
+ describe('formatBytes', () => {
5
+ it('should format 0 bytes correctly', () => {
6
+ expect(formatBytes(0)).toBe('0 B')
7
+ })
8
+
9
+ it('should format bytes correctly', () => {
10
+ expect(formatBytes(512)).toBe('512 B')
11
+ })
12
+
13
+ it('should format KB correctly', () => {
14
+ expect(formatBytes(1024)).toBe('1 KB')
15
+ expect(formatBytes(2048)).toBe('2 KB')
16
+ })
17
+
18
+ it('should format MB correctly', () => {
19
+ expect(formatBytes(1048576)).toBe('1 MB')
20
+ })
21
+
22
+ it('should format decimals correctly', () => {
23
+ expect(formatBytes(1536)).toBe('1.5 KB')
24
+ })
25
+ })
@@ -0,0 +1,59 @@
1
+ import type {SanityClient} from 'sanity'
2
+ import type {Asset, ReferencedDocument} from '@/types'
3
+
4
+ /**
5
+ * Checks where a specific asset is used in the studio.
6
+ */
7
+ export async function getAssetUsage(
8
+ sanityClient: SanityClient,
9
+ assetId: string,
10
+ ): Promise<ReferencedDocument[]> {
11
+ const query = `*[references($assetId)] {
12
+ _id,
13
+ _type,
14
+ "title": coalesce(title, name, label, _id)
15
+ }`
16
+
17
+ return await sanityClient.fetch(query, {assetId})
18
+ }
19
+
20
+ /**
21
+ * Finds all assets that are not referenced by any other document.
22
+ */
23
+ export async function findUnusedAssets(sanityClient: SanityClient): Promise<Asset[]> {
24
+ const query = `*[
25
+ _type in ["sanity.imageAsset", "sanity.fileAsset"] &&
26
+ count(*[references(^._id)]) == 0
27
+ ] {
28
+ _id,
29
+ _type,
30
+ url,
31
+ size,
32
+ extension,
33
+ mimeType,
34
+ originalFilename,
35
+ metadata { dimensions }
36
+ }`
37
+
38
+ return await sanityClient.fetch(query)
39
+ }
40
+
41
+ /**
42
+ * Bulk delete assets.
43
+ */
44
+ export async function deleteAssets(sanityClient: SanityClient, assetIds: string[]) {
45
+ const transaction = sanityClient.transaction()
46
+ assetIds.forEach((id) => transaction.delete(id))
47
+ return await transaction.commit()
48
+ }
49
+
50
+ /**
51
+ * Update asset original filename.
52
+ */
53
+ export async function updateAssetFilename(
54
+ sanityClient: SanityClient,
55
+ assetId: string,
56
+ filename: string,
57
+ ) {
58
+ return await sanityClient.patch(assetId).set({originalFilename: filename}).commit()
59
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Formats bytes to human readable format.
3
+ */
4
+ export const formatBytes = (bytes: number) => {
5
+ if (bytes === 0) return '0 B'
6
+ const k = 1024
7
+ const sizes = ['B', 'KB', 'MB', 'GB']
8
+ const i = Math.floor(Math.log(bytes) / Math.log(k))
9
+ return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
10
+ }