private bool UploadFile(string filePath, IFolder remoteFolder) // TODO make SyncItem
{
SleepWhileSuspended();
var syncItem = database.GetSyncItemFromLocalPath(filePath);
if (null == syncItem)
{
syncItem = SyncItemFactory.CreateFromLocalPath(filePath, false, repoInfo, database);
}
try
{
IDocument remoteDocument = null;
byte[] filehash = { };
// Prepare properties
string remoteFileName = syncItem.RemoteLeafname;
Dictionary<string, object> properties = new Dictionary<string, object>();
properties.Add(PropertyIds.Name, remoteFileName);
properties.Add(PropertyIds.ObjectTypeId, "cmis:document");
properties.Add(PropertyIds.CreationDate, File.GetCreationTime(syncItem.LocalPath));
properties.Add(PropertyIds.LastModificationDate, File.GetLastWriteTime(syncItem.LocalPath));
// Prepare content stream
using (Stream file = File.Open(syncItem.LocalPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (SHA1 hashAlg = new SHA1Managed())
using (CryptoStream hashstream = new CryptoStream(file, hashAlg, CryptoStreamMode.Read))
{
ContentStream contentStream = new ContentStream();
contentStream.FileName = remoteFileName;
contentStream.MimeType = MimeType.GetMIMEType(remoteFileName);
contentStream.Length = file.Length;
contentStream.Stream = hashstream;
Logger.InfoFormat("Uploading: {0} as {1}/{2}",syncItem.LocalPath, remoteFolder.Path, remoteFileName);
remoteDocument = remoteFolder.CreateDocument(properties, contentStream, null);
Logger.InfoFormat("Uploaded: {0}", syncItem.LocalPath);
filehash = hashAlg.Hash;
}
// Get metadata. Some metadata has probably been automatically added by the server.
Dictionary<string, string[]> metadata = FetchMetadata(remoteDocument);
// Create database entry for this file.
database.AddFile(syncItem, remoteDocument.Id, remoteDocument.LastModificationDate, metadata, filehash);
Logger.Info("Added file to database: " + syncItem.LocalPath);
return true;
}
catch (Exception e)
{
ProcessRecoverableException("Could not upload file: " + syncItem.LocalPath, e);
return false;
}
}