by Keyvan Nayyeri via Keyvan Nayyeri on 6/1/2007 4:01:41 PM
It seems that my CS Dev Guides aren't updated for a while! Let me return to CS Dev Guide with a post about file gallery API. At first glance, file gallery API appears to be different from other APIs but this difference is only and only in naming.
As you saw previously in my forgotten CS Dev Guide series, each section is derived from CommunityServer.Components.Section base class and is named based on that particular application for example, Weblog or Forum. On the other hand, individual items for an application are derived from CommunityServer.Components.Post class and have gotten a prefix with the name of that application for example, WeblogPost or GalleryPost.
But in file gallery things are different and we deal with some other names. In file gallery, we work with folders and entries. Folders are a way to categorize download items and entries are a representation of individual download items. Each entry belongs to a folder. Code equivalent of folder is CommunityServer.Files.Components.Folder which is inherited from Section and code equivalent of entry is CommunityServer.Files.Components.Entry which is inherited from Post base class. Download files are added to Entry objects as attachments. File comments have their own class at CommunityServer.Files.Components.EntryComment and that is derived from Post base class as well.
There are also two method factories at CommunityServer.Files.Components.Folders and CommunityServer.Files.Components.Entries to work with these two classes. Like other applications, there is a CommunityServer.Files.Components.FileGalleryThreadQuery available to you to use it and select file gallery entries. Unlike my older posts, I want to escape from describing all static methods in these two method factories and move on quicker. Rather than this, I just give a few examples of Folder and Entry and how to use them in action.
In first example, I want to find a folder with maximum amount of disk usage and return its Folder object from GetMaxDiskUsageFolder() method. To do this, I iterate through all folders by using Folders.GetFolders() method to find the folder.
Folder GetMaxDiskUsageFolder()
{
long max = long.MinValue;
Folder maxDiskUsage = new Folder();
foreach (Folder folder in Folders.GetFolders())
if (folder.DiskUsage > max)
max = folder.DiskUsage;
maxDiskUsage = folder;
}
return maxDiskUsage;
In GetMaxDownloadsForCurrentFolder() method (I know, I have to choose a longer name!!), I want to find the maximum downloads number for all entries in the current folder. Using a FileGalleryThreadQuery object, I select all entries in current folder and sort them by the number of downloads descending then call Entries.GetEntries() and Entries.GetEntry() methods to create an instance of the entry with maximum number of downloads and return its downloads number.
int GetMaxDownloadsForCurrentFolder()
FileGalleryThreadQuery query = new FileGalleryThreadQuery();
query.SectionID = CSContext.Current.SectionID;
query.SortBy = EntriesSortBy.Downloads;
query.SortOrder = SortOrder.Descending;
ThreadSet set = Entries.GetEntries(query);
Post maxDownloadsPost = set.Threads[0] as Post;
Entry maxDownloadsEntry = Entries.GetEntry(maxDownloadsPost.PostID);
return maxDownloadsEntry.Downloads;
The last and third example is about creating a download entry on fly. First I create a PostAttachment for my download then create a new instance of Entry object and set its required properties and store my attachment temporary. In the next step, I save my entry using Entries.Create() method and at the end store my attachment permanently.
void CreateNewEntry(string path)
CSContext context = CSContext.Current;
PostAttachment attachment = new PostAttachment();
attachment.Content = new MemoryStream(File.ReadAllBytes(path));
attachment.ContentType = "application/zip";
attachment.DateCreated = DateTime.Now;
attachment.FileName = Path.GetFileName(path);
attachment.SectionID = context.SectionID;
Entry entry = new Entry();
entry.Subject = "Sample Entry";
entry.Body = "<p>This entry is added from API</p>";
entry.PostDate = DateTime.Now;
entry.Attachment = attachment;
Guid id = Guid.NewGuid();
PostAttachments.AddTemporary(id, attachment);
Entries.Create(CSContext.Current.SectionID, entry, id);
PostAttachments.Add(entry, attachment);
Original Post: CS Dev Guide: File Gallery
The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.