开发者

Converting querystring to filename - best practise?

开发者 https://www.devze.com 2023-01-28 17:20 出处:网络
I have a handler (c#/asp.net) that pulls images from a database. The current format looks like foo.com/_image.ashx?querystring1&querystring2

I have a handler (c#/asp.net) that pulls images from a database. The current format looks like foo.com/_image.ashx?querystring1&querystring2 The querystring keys are things like id, width, zoom level etc.

I can either munge this up into a filename (i.e. foo.com/id__999_w__128__h__200.jpg), or a whole url structure (i.e. foo.com/id/999/w/128/h/2开发者_如何学运维00/image.jpg).

Im interested to see what other people would do given this situation, as there dont seem to be any articles or discussions of this practice on the net.


The easiest solution would be to base64 the filename, then you can pretty much guarantee the filename will be correct each time, and not tampered with or broken.

string filename = @"C:\images\myimage.png";

// You may need to change the Encoding type here
string converted = Convert.ToBase64String(
    System.Text.Encoding.Default.GetBytes(filename));


Best practices are a tricky thing, a lot of the choices you'll want to make depend on your situation.

Today a good way to handle this, is to load the images into a server like Amazon S3 instead of storing them into your database. Amazon S3 has an api that you can program to, so the application that you've already written that is storing the images into the database can be tweaked to upload them into Amazon S3 instead. Then you will store the url to the image in your database.

This will solve a number of problems.
1. Images stored on Amazon S3 can be exposed on Amazon CloudFront which is a CDN and will help you to route the image data to your end users in the fast possible route over the internet. 2. You can apply expires headers to these images so they cache on the client browser and the user will get optimum performance while using your website. In your curren mechanism you would have to add these headers programmatically. 3. Pulling blob data in and out of databases has never been the most performant operation against a database so you would be able to get rid of this code completely. 4. You can apply a cookie-free domain name to these images so that needless cookie passing is prevented and will also have a slight performance increase for your users. For example you might host your website on www.mysite.com, for your images you could assign the name images.mysitestatic.com, and this way you know that you don't have any cookies on the mysitestatic.com domain name that your user would need to upload and download on each request.

There are many advantages to hosting you images on a server as opposed to data pass through like your _images.ashx. As far as the urls you presented above, they both are pretty much the same. Unless you are looking to get seo value out of them it doesn't really matter. The url depends on your goals for performance/caching/seo, etc.

Hope this helps. Mark


Sorry, I'm new to this but I wanted to comment on the answer above.

I don't understand what you gain by BASE64 encoding the url? It won't make the url shorted, the base64 encoded string will usually be about 33% longer.

Here is an example: C:\images\myimage.png

Base64 encoded: QzpcaW1hZ2VzXG15aW1hZ2UucG5n

If you wanted to keep the url short, and you wanted to make sure the filename is correct per the solution above, you should just use the filename itself. The base64 encoded version isn't offering you any benefit.

Although, I'm not sure this helps you because its not related to you database solution at all. unless you were to store "images/myimage.png" in a field in your database and have your url look as follows: foo.com/images/image.jpg?h=5&w=10

With that format, the name of the resource being requested, is separated from the representation of the image at a height of 5 and width of 10.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号