| 
									
										
										
										
											2025-03-14 19:50:24 -05:00
										 |  |  | package nomino | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2025-03-15 20:09:38 -05:00
										 |  |  | 	"crypto" | 
					
						
							| 
									
										
										
										
											2025-03-19 18:05:16 -05:00
										 |  |  | 	"encoding/hex" | 
					
						
							| 
									
										
										
										
											2025-03-14 19:50:24 -05:00
										 |  |  | 	"errors" | 
					
						
							|  |  |  | 	"hash" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/gosimple/slug" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-19 18:05:16 -05:00
										 |  |  | // ErrMissingOriginal is the error returned by Slug if there is no filename. | 
					
						
							| 
									
										
										
										
											2025-03-14 19:50:24 -05:00
										 |  |  | var ErrMissingOriginal = errors.New("missing original filename") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func getOriginal(c *Config) (string, error) { | 
					
						
							|  |  |  | 	if c.original == "" { | 
					
						
							|  |  |  | 		return "", ErrMissingOriginal | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	name := c.original | 
					
						
							|  |  |  | 	c.original = "" | 
					
						
							|  |  |  | 	return name, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Slug generates a name from the original filename. | 
					
						
							|  |  |  | // When this is used, the original filename will be removed from the final filename. | 
					
						
							|  |  |  | // If a language is specified, that may affect the resulting slug. | 
					
						
							|  |  |  | func Slug(lang ...string) Generator { | 
					
						
							|  |  |  | 	ret := slug.Make | 
					
						
							|  |  |  | 	if len(lang) > 0 { | 
					
						
							|  |  |  | 		ret = func(in string) string { | 
					
						
							|  |  |  | 			return slug.MakeLang(in, lang[0]) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return func(c *Config) (string, error) { | 
					
						
							|  |  |  | 		name, err := getOriginal(c) | 
					
						
							|  |  |  | 		return ret(name), err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-19 18:05:16 -05:00
										 |  |  | // HashingFunc is a function that generates a hash.Hash. | 
					
						
							| 
									
										
										
										
											2025-03-14 19:50:24 -05:00
										 |  |  | type HashingFunc func() hash.Hash | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-19 18:05:16 -05:00
										 |  |  | // New allows HashingFunc to be used as a Hasher. | 
					
						
							| 
									
										
										
										
											2025-03-15 20:09:38 -05:00
										 |  |  | func (hf HashingFunc) New() hash.Hash { | 
					
						
							|  |  |  | 	return hf() | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2025-03-14 19:50:24 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-15 20:09:38 -05:00
										 |  |  | // Hasher is a type returns a hash.Hash. | 
					
						
							|  |  |  | // All crypto.Hash may be used. | 
					
						
							|  |  |  | type Hasher interface { | 
					
						
							|  |  |  | 	New() hash.Hash | 
					
						
							| 
									
										
										
										
											2025-03-14 19:50:24 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-19 18:05:16 -05:00
										 |  |  | // ErrInvalidHash is returned by the Hash generator when an invalid HashType is passed. | 
					
						
							| 
									
										
										
										
											2025-03-15 20:09:38 -05:00
										 |  |  | var ErrInvalidHash = errors.New("invalid hash type") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-14 19:50:24 -05:00
										 |  |  | // Hash generates a name from a hash of the filename. | 
					
						
							|  |  |  | // When this is used, the original filename will be removed from the final filename. | 
					
						
							| 
									
										
										
										
											2025-03-15 20:09:38 -05:00
										 |  |  | func Hash(h Hasher) Generator { | 
					
						
							|  |  |  | 	if h == nil { | 
					
						
							|  |  |  | 		h = crypto.MD5 | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-03-14 19:50:24 -05:00
										 |  |  | 	return func(c *Config) (string, error) { | 
					
						
							| 
									
										
										
										
											2025-03-15 20:09:38 -05:00
										 |  |  | 		if h == crypto.MD5SHA1 { | 
					
						
							|  |  |  | 			return "", ErrInvalidHash | 
					
						
							| 
									
										
										
										
											2025-03-14 19:50:24 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		name, err := getOriginal(c) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return "", err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2025-03-15 20:09:38 -05:00
										 |  |  | 		hs := h.New() | 
					
						
							| 
									
										
										
										
											2025-03-14 19:50:24 -05:00
										 |  |  | 		hs.Write([]byte(name)) | 
					
						
							| 
									
										
										
										
											2025-03-19 18:05:16 -05:00
										 |  |  | 		return hex.EncodeToString(hs.Sum(nil)), nil | 
					
						
							| 
									
										
										
										
											2025-03-14 19:50:24 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |