How to store videos for a website? - c#

I am developing an ASP.NET website. In the website users needs to create an account and then they can upload videos of their work. Viewers can view it freely. I use html5 <video> tag to display videos. I am using sql server database. Now where to store the videos when the user uploads it? Should I store all the videos in same folder? If yes then there can be multiple videos of same name. How to deal with that?

A solution is to embed an id in the name, one way or another.
You can prefix it like this
343-Video.mp4, 344-Video.mp4, 345-How to make a sandwich.mp4
Or you can replace the entire thing with a guid and store all information in the database.
If you go with the guid, I would personally store all the videos in the same folder out of convinience, to make retrieval as simple as possible. A problem with this is SEO, search engines love file names and they don't like IDs in the names.
To get file names, you can either make folders and name them the IDs and put the videos in there or you can make a controller that will serve the files from an id. The simplests of those is by far to have folders with IDs as the names then have the videos in there, you can also fittingly store the same video of different formats in there.
/videos/343/How to make a sandwich.mp4, /videos/343/How to make a sandwich.webm
/videos/344/video.mp4, /videos/344/video.webm
That is if you have plenty of storage and want to use your own server, otherwise you can save a lot of server costs(Both storage and trafic) by using a service to host your videos, e.g. YouTube or Vimeo

You would want to tag the videos with some kind of ID associated with them, especially if they might have the same name. Basically, you'll store a record in a table that contains the information (who uploaded, upload date, video type, etc.) for the upload. You'll also want to store the path to the actual file on the file server where you're storing the videos and then use that path to view them.

upload videos in each user's folder or assign a random unique name to videos (like Guid) then add a row to the database indicating the actual name and folder of video file.

Look at this answer for filename coding: https://stackoverflow.com/a/1638761/844044
In SQL you keep a link between original filename (or the name you want to display) and the base64 filename you generate.
There might be collisions on the base64 filename you generate (this should be randomly generated) but that would be trivial to solve.
also: https://www.youtube.com/watch?v=gocwRvLhDf8, on the youtube filename

Related

MySQL storing images using hyperlinks

Good Day. So I have a question about storing images in Mysql.
My professor told us to change the data type of our column from blob to text.
And told us to store our images in a folder and not in the database itself and
just create a link stored in the column with the data type of text to link it to the directory of the images I want to show up in the db.
I don't know how to do it. I don't know how to use PHP.
My group is coding in C#.
Thank you!
Assume you are saving images in a directory /your_project/images/image_name.png
Then just save the relative path in your database like 'images/image_name.png'.
And when you need to fetch that image then just get this value and prep-and it with your main directory_name like www.site_name.com/images/image_name.png
You can change your main path when you move server .

HTML textarea with image upload

I'm currently in need of develop a website that allow user to input information and images into a <textarea> then save to database. Those data will be displayed in another page.
I though of using AJAX to upload file then append the <img> to <textarea>. But this approach will produce SQL injection security threat.
So I need an advice on how to achieve this but still can get rid of SQL injection.
When the user uploads an image, just do the following and you will be safe to append the image HTML to the textarea:
Is the user upload a valid JPG/GIF/PNG/x image? (Use image libraries to verify that.)
Rename the image to something "safe" like a CRC32 of its contents + the current time in microseconds so the file name is innocuous.
Put the image with its new name in a location that can be served.
you'll find you cant upload files using ajax - you will want to find a plugin for that. I'd reccomend looking at https://github.com/blueimp/jQuery-File-Upload/wiki - although it may have too many features. Theres even an mvc3 example for it! https://github.com/maxpavlov/jQuery-File-Upload.MVC3
After you upload the image, I suppose you can trigger another ajax event to retrieve the image and then display it in a div next to the text area.
As far as security issues, I've been told allowing a user to upload a file is inherently insecure. see https://github.com/blueimp/jQuery-File-Upload/wiki/Security for more details.
My apologies for the excess links. Also, if you're storing many, many files with infrequent access, you may want to consider saving the files to disk. ie. write them to a network location and store the filename in a table (save the file on disk with a GUID).
ie. Table UserFileLocations
PK | UserFileName | DiskID
1..n | tree.jpg | //ServerPath/Folder/103c-aa34-0ac2-01cd
...

Display thumbnails for images in articles

So I have this website where users can post articles, each article containing at least one photo. What I want to do is when I display the list of articles on the website I want to also show a thumbnail next to the articles name.
Here comes the tricky part: the images are not hosted on my server, are simply links hosted on some image-hosting website. Another problem is that I don't know where the images appear in the post (they could be at the beginning, at the end or in the middle of the article).
What would be the best approach to create a thumbnail system in this case?
I was thinking maybe I could do this: every time an article is posted or edited and stored into the database I could scan the entire articles for images links and store the first link in a separate value in the database (this could be kind of slow though).
Also once I have those values stored and I have to display a thumbnail the only way to do so will be by showing the full image resized to the thumbnail size (that means the user has to download multiple full-size images to see the articles list with thumbnails).
Is there any better approach? (you can see the technologies used in the tags)
Create a thumbnails task that runs in the background after an article has been published.
Find image tags in the article HTML using regular expression.
Get those images and create and thumbnail that you save locally in a folder in your server.
Protect that folder/location against hotlinking.
Use those local pictures as thumbnails
Use HtmlAgilityPack as a starter to get to the images from the image host.
Use an ASP.NET handler to generate the thumbnails. That way, you won't have to store anything locally, the thumbnails images will only exist in memory, making hotlinking impossible

Architecture to Save word documents with search options

We are building an internal application where users have the option to save word documents in the system,But the issue is the users should have the ability to search for these documents by keywords.
We use asp.net,c# and Sqlserver 2008.I was wondering to save these documents in a Varchar field and then searching these fields for keywords or do i need to use full text search using Solr/Lucene.
I would like to know if this is the efficient design for this purpose.
Thanks in advance !
If you have to store word documents in the database and you wish to be able to search them by some classic keywords then use a Virtual Path Provider, each time the document is saved put some keywords in a dB field and search using those keywords. This method will get around the DB Copy that John3136 mentioned.
If you need to be able to search on the content of the documents you wont be able to do that if the files are saved as blobs, so for this purpose it may make more sense to save the documents as XML Word 2003 and configure a Full Text search to ignore angle brackets, eg:
Regex.Replace(dBFieldOfWordXMLData, #"<[^>]*>", string.Empty);
I think the most efficient way is to use a Virtual Path Provider, MSDN Articles and Sharepoint documents use Virtual Path Provider and they are searchable. I've done some research on what the most efficient solution would be came across EpiServer CMS on Azure: http://episerverazurevpp.codeplex.com/
Without more details this is impossible to answer sensibly. A few things to consider:
Are you saying save the whole doc into a varchar field in a DB? That doesn't really sound smart - you have the whole problem of keeping the DB copy in sync with the disc copy (not to mention the whole idea of a DB copy in the first place...)
You mention keywords: If there are a limited number of keywords then it is fairly easy to write an office interop app that searches a word doc for keywords. You could either do this on save and keep a DB of which docs contain which words, or you could do it "on the fly" (i.e. an app that searches a whole folder full of docs for the ones containing a particular word) - it all depends on how many docs you are likely to have, required performance etc.
Could you do something with the document properties (add your own custom property corresponding to a keyword) and search for files with that property?

How can I read an xml file from the users computer on an ASP.NET MVC2 site?

I am writing a website to consolidate a bunch of XML files with data into one MySQL database. I need to have a way to allow users to select a directory on their computer that contains a bunch of xml files. The site then reads each of those files and takes care of consolidating the information.
Is there a simple way (like the default open file dialog for win forms and wpf) to bring up a file dialog on a users computer, let the user pick a directory, and then be able to read the xml files in the selected directory? Would I have to upload them to the site temporarily first? Or could I just access them on the users computer?
Thanks!!
You can't access files from a webserver directly. You would need to write an ActiveX Control if you really don't find another way.
The standard conform way it just uploading one or more files with the browser fileupload:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.aspx
I would suggest that the user should zip the files and just upload the zip file.
There are some hacks - but I don't think it fits:
http://the-stickman.com/web-development/javascript/upload-multiple-files-with-a-single-file-element/
http://dotnetslackers.com/articles/aspnet/Upload_multiple_files_using_the_HtmlInputFile_control.aspx
I think you have to have a web dialog to upload the files to a temp location like you already mentioned and do the consolidation there before committing to your database. Or, maybe you can do the consolidation in JavaScript in the user's browser instance.

Categories