| 
									
										
										
										
											2019-11-10 13:14:49 +03:00
										 |  |  | // Copyright © 2018 Inanc Gumus | 
					
						
							|  |  |  | // Learn Go Programming Course | 
					
						
							|  |  |  | // License: https://creativecommons.org/licenses/by-nc-sa/4.0/ | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // For more tutorials  : https://learngoprogramming.com | 
					
						
							|  |  |  | // In-person training  : https://www.linkedin.com/in/inancgumus/ | 
					
						
							|  |  |  | // Follow me on twitter: https://twitter.com/inancgumus | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2019-11-11 15:29:32 +03:00
										 |  |  | 	"bytes" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2019-11-10 13:14:49 +03:00
										 |  |  | 	"io" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-11 15:29:32 +03:00
										 |  |  | // reader reads from `r` if the stream starts with a given signature. | 
					
						
							| 
									
										
										
										
											2019-11-10 13:14:49 +03:00
										 |  |  | // otherwise it stops and returns with an error. | 
					
						
							|  |  |  | type signatureReader struct { | 
					
						
							| 
									
										
										
										
											2019-11-11 15:29:32 +03:00
										 |  |  | 	r         io.Reader // reads from the response.Body (or from any reader) | 
					
						
							|  |  |  | 	signature []byte    // stream should start with this initial signature | 
					
						
							| 
									
										
										
										
											2019-11-10 13:14:49 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Read implements the io.Reader interface. | 
					
						
							|  |  |  | func (sr *signatureReader) Read(b []byte) (n int, err error) { | 
					
						
							|  |  |  | 	n, err = sr.r.Read(b) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-11 15:29:32 +03:00
										 |  |  | 	l := len(sr.signature) | 
					
						
							| 
									
										
										
										
											2019-11-10 13:14:49 +03:00
										 |  |  | 	if l == 0 { | 
					
						
							| 
									
										
										
										
											2019-11-11 15:29:32 +03:00
										 |  |  | 		// simply return if the signature has already been detected. | 
					
						
							| 
									
										
										
										
											2019-11-10 13:14:49 +03:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-11 15:29:32 +03:00
										 |  |  | 	// 1) buffer   : **** -> b[:3]            -> *** | 
					
						
							|  |  |  | 	//    signature: ***  -> sr.signature[:3] -> *** | 
					
						
							|  |  |  | 	// 2) buffer   : **   -> b[:2]            -> ** | 
					
						
							|  |  |  | 	//    signature: **** -> sr.signature[:2] -> ** | 
					
						
							|  |  |  | 	if lb := len(b); lb < l { | 
					
						
							| 
									
										
										
										
											2019-11-10 13:14:49 +03:00
										 |  |  | 		l = lb | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-11 15:29:32 +03:00
										 |  |  | 	if got, want := b[:l], sr.signature[:l]; !bytes.Equal(got, want) { | 
					
						
							|  |  |  | 		err = fmt.Errorf("signature doesn't match, got: % x, want: % x", got, want) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Assuming the `len(b)` is 4. | 
					
						
							|  |  |  | 	// 1st Read(): pr.signature[0:4] -> first part | 
					
						
							|  |  |  | 	// 2nd Read(): pr.signature[0:4] -> second part | 
					
						
							|  |  |  | 	sr.signature = sr.signature[l:] | 
					
						
							|  |  |  | 	return n, err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // create a reader for detecting only the png signatures. | 
					
						
							|  |  |  | func pngReader(r io.Reader) io.Reader { | 
					
						
							|  |  |  | 	return &signatureReader{ | 
					
						
							|  |  |  | 		r:         r, | 
					
						
							|  |  |  | 		signature: []byte("\x89PNG\r\n\x1a\n"), | 
					
						
							| 
									
										
										
										
											2019-11-10 13:14:49 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |