| 
									
										
										
										
											2025-03-19 18:45:36 -05:00
										 |  |  | package nomino_test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"codeberg.org/danjones000/nomino" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Define a global Config. | 
					
						
							|  |  |  | func NominoConfig() nomino.Config { | 
					
						
							|  |  |  | 	return nomino.NewConfig( | 
					
						
							|  |  |  | 		nomino.WithSeparator("-"), | 
					
						
							|  |  |  | 		nomino.WithPrefix("upload"), | 
					
						
							|  |  |  | 		nomino.WithGenerator(nomino.UUID( | 
					
						
							|  |  |  | 			nomino.UUIDv7, | 
					
						
							|  |  |  | 		)), | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-23 10:52:54 -05:00
										 |  |  | // HandleImgUploads generates new filenames for images with a png extension. | 
					
						
							| 
									
										
										
										
											2025-03-19 18:45:36 -05:00
										 |  |  | func HandleImgUploads(orig string) string { | 
					
						
							| 
									
										
										
										
											2025-03-23 10:52:54 -05:00
										 |  |  | 	// Here, we use nomino.Make function to generate the filename. | 
					
						
							|  |  |  | 	// We use our global config, and add in a few extra Options specific to this. | 
					
						
							| 
									
										
										
										
											2025-03-19 18:45:36 -05:00
										 |  |  | 	newName, _ := nomino.Make( | 
					
						
							|  |  |  | 		NominoConfig(), | 
					
						
							|  |  |  | 		nomino.WithExtension("png"), | 
					
						
							|  |  |  | 		nomino.WithOriginalSlug(orig), | 
					
						
							|  |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return newName | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // HandleVidUploads generates a new filename for videos. | 
					
						
							|  |  |  | // We ignore the original filename and use a timestamp for the generated part | 
					
						
							|  |  |  | // with a webm extension. | 
					
						
							|  |  |  | func HandleVidUploads() string { | 
					
						
							| 
									
										
										
										
											2025-03-23 10:52:54 -05:00
										 |  |  | 	// Because we're using a different Generator, we chose to use the Make method on the Generator. | 
					
						
							|  |  |  | 	// We add in additional Options with the `AddOptions` method on the `Config` | 
					
						
							| 
									
										
										
										
											2025-03-19 18:45:36 -05:00
										 |  |  | 	newName, _ := nomino.Timestamp(nomino.TimestampUTC()). | 
					
						
							|  |  |  | 		MakeWithConfig(NominoConfig().AddOptions( | 
					
						
							|  |  |  | 			nomino.WithExtension("webm"), | 
					
						
							|  |  |  | 		)) | 
					
						
							|  |  |  | 	return newName | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-23 10:52:54 -05:00
										 |  |  | // This example shows how to use nomino. | 
					
						
							| 
									
										
										
										
											2025-03-19 18:45:36 -05:00
										 |  |  | func Example() { | 
					
						
							|  |  |  | 	// Pretend we have an image upload | 
					
						
							|  |  |  | 	filename := "George" | 
					
						
							|  |  |  | 	uploadImgName := HandleImgUploads(filename) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Upload to storage | 
					
						
							|  |  |  | 	fmt.Println(uploadImgName) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// New Video Upload | 
					
						
							|  |  |  | 	uploadVidName := HandleVidUploads() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Upload to storage | 
					
						
							|  |  |  | 	fmt.Println(uploadVidName) | 
					
						
							|  |  |  | } |