📜 ⬆️ ⬇️

Organizing sharing of files in C # application

Recently I had to work on an interesting project in which the customer asked to implement file sharing over the Internet, respectively, with direct access directly from the application.

Primary requirements:

The first thing that came to mind was to use cloud data storage with API access. So what came of it?

First problems


I decided to study the API documentation of such services as:

At first I was satisfied with all the vaults. In each, there were methods for downloading, direct downloading, deleting, displaying a list of files, as well as snippets and even a library of API interaction for NET. platforms. It seems to be all you need! But there were some critical moments for me, for example, DropBox set a limit on the number of requests to the API - 5000 per day. And the main problem of storage (in my situation): DropBox, Box, Windows Live SkyDrive in the way of authorization of client applications - OAuth. This type of authorization does not allow you to directly transfer account information to the account. Only one Ge.tt bidder remains.
')

Files for all


Ge.tt provides the user with a free 2GB storage. To interact with the cloud, you need to create an application and get APIKey . The documentation is rather poor, but all the necessary functions are there. Terms of use fully comply with customer requirements. The authorization model is simple: POST request with Email + Password + APIToken parameters . Bonus to this, ready dynamic library with the implementation of the necessary functions, with it we will work.

Implementation


The storage structure is defined as follows:


That is, each ball can include a list of files, so you can create thematic sections corresponding to, for example, the file extension.
We describe the main algorithm:
  1. Authorization
  2. Upgrade ball.
  3. Getting a list of files in the balls.
  4. Download file.


Authorization

We connect the library to our project:
using Gett; 

  //  Gett.Sharing.GettUser user = new Gett.Sharing.GettUser(20);//   .   2 user.Login("qbxpm1maish5tgwjivqnl0a88jv2t9tim", "example@mail.com", "dj47vn3oc7"); 

For security, I do not recommend to keep the username and password in its pure form, use encryption.

Get the files

As I wrote, to get a list of files, you must select a specific ball. We draw the list of the ball in listBoxShare, then when clicking on an element we will load the list of files in listBoxFiles:
 user.RefreshMe();//POST  this.listBox_Share.Items.Clear(); this.listBox_Files.Items.Clear(); this.listBox_Share.Items.AddRange(user.Shares.GetShares()); //    listBox // Gett.Sharing.GettShare[] _shares = user.Shares.GetShares(); //    ,   

Getting information about files is a similar process. In the SelectedIndexChanged event of the listBox_Share, we write the code:
 private void listBoxShare_SelectedIndexChanged(object sender, EventArgs e) { this.listBox_Files.Items.Clear(); if (this.listBox_Share.SelectedItem is Gett.Sharing.GettShare)//    { Gett.Sharing.GettShare share = (Gett.Sharing.GettShare)this.listBox_Share.SelectedItem;//    this.listBox_Files.Items.AddRange(share.Files);//       listBox_Files List<Gett.Sharing.GettFile> FileList = new List<Gett.Sharing.GettFile>();//        } } 

It should be remembered that we get a list of files that contains:


To get the necessary properties, you need to refer to the necessary element, and call the Info property:

 string name= FileList[0].Info.FileName;//      long size = FileList[0].Info.Size;//  


Now download the file. We write the code for the same SelectedIndexChanged:

 if (this.listBox_Files.SelectedItem is Gett.Sharing.GettFile)//    { Gett.Sharing.GettFile file = (Gett.Sharing.GettFile)this.listBox_Files.SelectedItem;//   SaveFileDialog saveDialog = new SaveFileDialog();// saveDialog    saveDialog.OverwritePrompt = true; saveDialog.FileName = file.Info.FileName; if (saveDialog.ShowDialog() == DialogResult.OK) { file.DownloadFileAsync(saveDialog.FileName);//   } 


Further business of your imagination: delete, rename files, create directories. API opens wide possibilities for the implementation of any ideas. A good idea is the key to success!

Source: https://habr.com/ru/post/141705/


All Articles