Critique my image folder and image database implementation

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
Hace 14 años
Hi,

I have a friend/client that I am switching over to nopCommerce.  His current shop uses a directory/folder structure for storing product images.  Needless to say uploading all those images to the database will be a big job, not to mention the question of whether to store images in the db at all.  To be honest I have no idea which way is better so I decided to try to implement both solutions.  

It feels a bit hacky/work-aroundish but I am interested to see what anyone has to say about the fundamental pros and cons of my implementation.  My goals were simple:
1. Change as little of the existing code as possible to make upgrades easier.
2. Utilize the resizing methods in picture manager.
3. Support binary images AND image folders.

Here is what I did:

1. In the db I added a new column to Nop_Picture called URL and allowed Nulls.
2. Changed PictureBinary column to allow Nulls.
3. Added 2 sprocs called Cstm_InsertPicture and Cstm_UpdatePicture.
4. In SqlPictureProvider.cs added public overrides for InsertPictureURL and UpdatePictureURL that use the additional URL column. Also added a reference to picture.URL in GetPictureFromReader().
5. Added public abstracts to DBPictureProvider.cs matching Insert and UpdateURL.
6. Added get;set; for URL property in Picture.cs
7. PictureManager.cs
-Add mapping to DBMapping
-Add Insert/UpdateURL public static methods.
-Add if (picture.binary != null) just above the 'using (MemoryStream.. in GetPictureURL(pciture,targetsize,getdefaultURL)
***-Add else statement below and copied same code sans removing the memory stream reference.  Instead I created an Image object and used Image.FromFile(picture.URL)
8. Just to test the uploading of images I added a checkbox(Picture is in file?) and 2 textboxes(File Location),(File Extension) to /Admin/ProductPictures.ascx.
9. In the code behind I added an Event to toggle the visibility of the FileUpload field and the textbox fields based on whether the checkbox was checked.
10. Finally I added another condition (cbIsInFile.Checked == false) to the 2nd if statement in btnUploadProductPicture_Click.  Then added an else that used my InsertPictureURL I defined in the previous steps.

That's it.  It's not terribly elegant but it works.  My biggest concern is whether resizing images from a file takes more overhead or can cause problems etc.  Frankly I have no idea.   Also, there is I am sure a much better way to upload the images than statically entering in the file location (\images\logo.jpg) I just haven't messed with that yet.

Needless to say uploading the image url's was much easier than converting the images to binary and I can use both the db and directories to store images.

Comments good or bad appreciated.
joe
Hace 14 años
Oh, and one more thing.

I did have a problem in PictureManager.cs in the above referenced GetPictureURL.

The 'if (TargetSize == 0)' caused me some problems because the File.WriteAllBytes method needs an binary input.  I did not see the point of converting my image file into binary just to pass it through this part of GetPictureURL.  Instead I set the TargetSize in /Admin/ProductPictures.ascx.cs to 175.  This bypasses the problem and because I want defined sizes for my images everywhere on the site I don't need this.

joe
Hace 14 años
You'll be able to store pictures on a file system in the next release (this option will be configurable)
Hace 14 años
Any hints into how it will work?
Hace 14 años
nopCommerce team | a.m. wrote:
You'll be able to store pictures on a file system in the next release (this option will be configurable)



Next version means 1.5 or 1.6 as of March 11, 2010? I am sorry I can't find a tick box to choose either file system or database.

Thanks
Hace 14 años
oyoon wrote:



Next version means 1.5 or 1.6 as of March 11, 2010? I am sorry I can't find a tick box to choose either file system or database.

Thanks


I guess by next version Andrei meant nopCommerce1.6 version
Hace 14 años
Possibly 1.6.

The 1.5 version appears to have a setting in the admin section that sets the photo location to database true/false.  It appears it is a either/or type of setting.  I don't think you can use both at this time.

The tick box I referred to is in a custom implementation I made.  It really is nothing more than an 'if(box.checked = true)'then add file location to database and leave picturebinary null.  The general steps for what I did are in my original post.

joe
Hace 14 años
the setting is :

Media.Images.StoreInDB


You can find it under Global settings\All Settings

page through and find the Name : Media.Images.StoreInDB

edit it and set it to true/false as you need.

be carefull not to switch after you added produt in wither ways. the store would not be able to find the pics added before the switch.
Hace 14 años
...beautiy.....I found the setting key. then I don't have to modify the upload file provider anymore.

Thanks
Hace 14 años
So did I get this right?  In nop 1.50 we have the option of storing images in the db or file structure?
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.