11import { strapi } from '@strapi/client' ;
22import * as dotenv from 'dotenv' ;
3+ import * as os from 'os' ;
34dotenv . config ( ) ;
45
56const api_token = process . env . FULL_ACCESS_TOKEN ; // READ_ONLY_TOKEN is also available
@@ -74,10 +75,13 @@ async function runDemo() {
7475 await demonstrateBasicCategoryFunctionality ( ) ;
7576 await demonstrateCategoryImageInteractions ( ) ;
7677 await demonstrateDirectFileOperations ( ) ;
78+ await demonstrateFileUpdates ( ) ;
7779}
7880
7981async function demonstrateBasicCategoryFunctionality ( ) {
80- console . log ( '\n=== Basic Category Data ===\n' ) ;
82+ console . log ( os . EOL ) ;
83+ console . log ( '=== Basic Category Data ===' ) ;
84+ console . log ( os . EOL ) ;
8185
8286 const categories = client . collection ( 'categories' ) ;
8387
@@ -90,7 +94,9 @@ async function demonstrateBasicCategoryFunctionality() {
9094}
9195
9296async function demonstrateCategoryImageInteractions ( ) {
93- console . log ( '\n=== Categories with their images ===\n' ) ;
97+ console . log ( os . EOL ) ;
98+ console . log ( '=== Categories with their images ===' ) ;
99+ console . log ( os . EOL ) ;
94100
95101 const categories = client . collection ( 'categories' ) ;
96102
@@ -115,7 +121,9 @@ async function demonstrateCategoryImageInteractions() {
115121}
116122
117123async function demonstrateDirectFileOperations ( ) {
118- console . log ( '\n=== Direct file queries ===\n' ) ;
124+ console . log ( os . EOL ) ;
125+ console . log ( '=== Direct file queries ===' ) ;
126+ console . log ( os . EOL ) ;
119127
120128 const categories = client . collection ( 'categories' ) ;
121129
@@ -139,7 +147,9 @@ async function demonstrateDirectFileOperations() {
139147
140148 // Get the specific file by ID
141149 const fileInfo = ( await client . files . findOne ( imageId ) ) as unknown as FileAttributes ;
142- console . log ( '\nFile details:' ) ;
150+
151+ console . log ( os . EOL ) ;
152+ console . log ( 'File details:' ) ;
143153 console . log ( ` Name: ${ fileInfo . name } ` ) ;
144154 console . log ( ` Alternative Text: ${ fileInfo . alternativeText || 'None' } ` ) ;
145155 console . log ( ` Caption: ${ fileInfo . caption || 'None' } ` ) ;
@@ -152,6 +162,66 @@ async function demonstrateDirectFileOperations() {
152162 }
153163}
154164
165+ async function demonstrateFileUpdates ( ) {
166+ console . log ( os . EOL ) ;
167+ console . log ( '=== File Update Operations ===' ) ;
168+ console . log ( os . EOL ) ;
169+
170+ const categories = client . collection ( 'categories' ) ;
171+
172+ // Get a specific category using find with a filter
173+ const techCategoryResult = ( await categories . find ( {
174+ filters : {
175+ slug : {
176+ $eq : 'tech' ,
177+ } ,
178+ } ,
179+ populate : [ 'image' ] ,
180+ } ) ) as unknown as CategoryResponse ;
181+
182+ if ( techCategoryResult . data && techCategoryResult . data . length > 0 ) {
183+ const categoryData = techCategoryResult . data [ 0 ] ;
184+
185+ // Only proceed if the category has an image
186+ if ( categoryData . image ) {
187+ const imageId = categoryData . image . id ;
188+ console . log ( `Working with image: ${ categoryData . image . name } (ID: ${ imageId } )` ) ;
189+
190+ // Update the file metadata
191+ // For demo purposes, we'll update the alternative text and caption
192+ const updatedAltText = `Updated alt text for ${ categoryData . image . name } - ${ new Date ( ) . toISOString ( ) } ` ;
193+ const updatedCaption = `Updated caption - ${ new Date ( ) . toISOString ( ) } ` ;
194+
195+ console . log ( os . EOL ) ;
196+ console . log ( 'Updating file metadata...' ) ;
197+ console . log ( ` New Alt Text: ${ updatedAltText } ` ) ;
198+ console . log ( ` New Caption: ${ updatedCaption } ` ) ;
199+
200+ try {
201+ const updatedFile = await client . files . update ( imageId , {
202+ alternativeText : updatedAltText ,
203+ caption : updatedCaption ,
204+ } ) ;
205+
206+ console . log ( os . EOL ) ;
207+ console . log ( 'File metadata updated successfully!' ) ;
208+ console . log ( ` Name: ${ updatedFile . name } ` ) ;
209+ console . log ( ` Alternative Text: ${ updatedFile . alternativeText || 'None' } ` ) ;
210+ console . log ( ` Caption: ${ updatedFile . caption || 'None' } ` ) ;
211+ console . log ( ` Updated At: ${ new Date ( updatedFile . updatedAt ) . toLocaleString ( ) } ` ) ;
212+ } catch ( error ) {
213+ console . error ( 'Error updating file:' , error ) ;
214+ }
215+ } else {
216+ console . log ( 'No image associated with this category to update' ) ;
217+ }
218+ } else {
219+ console . log (
220+ 'Tech category not found. Make sure you have a category with slug "tech" in your Strapi instance.'
221+ ) ;
222+ }
223+ }
224+
155225// Helper function to format file sizes
156226function formatFileSize ( bytes : number ) : string {
157227 if ( bytes < 1024 ) return `${ bytes } B` ;
@@ -163,7 +233,8 @@ function formatFileSize(bytes: number): string {
163233// Run the demo
164234runDemo ( )
165235 . then ( ( ) => {
166- console . log ( '\nDemo completed successfully!' ) ;
236+ console . log ( os . EOL ) ;
237+ console . log ( 'Demo completed successfully!' ) ;
167238 } )
168239 . catch ( ( error ) => {
169240 console . error ( 'Error running demo:' , error ) ;
0 commit comments