Пример #1
0
        public void AddFile(Resource resource, byte[] fileInBytes,FileSource source)
        {
            //Check if we have a valid file
            Check(resource, fileInBytes);

            //See if we have file 
            if (_files.ContainsKey(resource.Id))
            {
                if (IsNewer(_files[resource.Id], resource))
                    UpdateFile(resource, fileInBytes, source);
                else return;
            }
            else
            {
                SaveToDisk(fileInBytes, resource);
                _files.Add(resource.Id, resource);
            }
                    
            //Check what the source is and who we should inform
            switch (source)
            {
                case FileSource.ActivityCloud:
                    if (FileDownloadedFromCloud != null)
                        FileDownloadedFromCloud(this, new FileEventArgs(resource));
                    break;
                case FileSource.ActivityManager:
                    if (FileAdded != null)
                        FileAdded(this, new FileEventArgs(resource));
                    break;
                case FileSource.ActivityClient:
                    if (FileCopied != null)
                        FileCopied(this, new FileEventArgs(resource));
                    break; 
            }
            Log.Out("FileStore", string.Format("Added file {0} to store", resource.Name), LogCode.Log);
        }   
 public void AddResource(Resource resource, string localPath)
 {
     var path = BaseUrl + resource.CloudPath + "?size=" +
                         resource.Size.ToString(CultureInfo.InvariantCulture) + "&creationTime=" +
                         resource.CreationTime
                         + "&lastWriteTime=" + resource.LastWriteTime + "&relativePath=" +
                         HttpUtility.UrlEncode(resource.RelativePath);
     Rest.UploadStream(path, localPath, ConnectionId).ContinueWith(r => Log.Out("ActivityCloudConnector", string.Format("Finished upload of {0}", resource.Name), LogCode.Log));
     Log.Out("ActivityCloudConnector", string.Format("Started upload of {0}", resource.Name), LogCode.Log);
 }
Пример #3
0
 private bool IsNewer(Resource resourceInFileStore, Resource requestedResource)
 {
     return false;
 }
Пример #4
0
 public void DownloadFile(Resource resource, string path, FileSource source, string _connectionId = null)
 {
     Rest.DownloadStream(path, _connectionId).ContinueWith(stream =>
     {
         Log.Out("FileStore", string.Format("Finished download for {0}", resource.Name), LogCode.Log);
         AddFile(resource, stream.Result, source);
     });
     Log.Out("FileStore", string.Format("Started download for {0}", resource.Name), LogCode.Log);
 }
Пример #5
0
 public void AddFile(Resource resource, Stream stream, FileSource source)
 {
     AddFile(resource, GetBytesFromStream(resource, stream), source);
 }
Пример #6
0
 public virtual void FileNetUploadRequest(Resource r)
 {
     if (FileUploadRequest != null)
         FileUploadRequest(this, new FileEventArgs(r));
 }
Пример #7
0
 /// <summary>
 /// Uploads a resource to the activity manager
 /// </summary>
 /// <param name="r"></param>
 private void UploadResource(Resource r)
 {
     Task.Factory.StartNew(
            delegate
            {
                 var uploader = new WebClient();
                 uploader.UploadDataAsync(new Uri(ServiceAddress + "Files/" + r.ActivityId + "/" + r.Id),
                                          File.ReadAllBytes(Path.Combine(_fileStore.BasePath, r.RelativePath)));
                 //_fileServer.BasePath + r.RelativePath);
                 Log.Out("ActivityClient", string.Format("Received Request to upload {0}", r.Name), LogCode.Log);
             });
 }
Пример #8
0
 public void Updatefile(Resource resource, byte[] fileInBytes)
 {
     Task.Factory.StartNew(
            delegate
            {
                 _files[resource.Id] = resource;
                 SaveToDisk(fileInBytes, resource);
                 if (FileChanged != null)
                     FileChanged(this, new FileEventArgs(resource));
                 Log.Out("FileStore", string.Format("FileStore: Updated file {0} in store", resource.Name), LogCode.Log);
             });
 }
Пример #9
0
 private byte[] GetBytesFromStream(Resource resource, Stream stream)
 {
     var buffer = new byte[resource.Size];
     var ms = new MemoryStream();
     int bytesRead;
     do
     {
         bytesRead = stream.Read(buffer, 0, buffer.Length);
         ms.Write(buffer, 0, bytesRead);
     } while (bytesRead > 0);
     ms.Close();
     return buffer;
 }
Пример #10
0
 public Stream GetStreamFromFile(Resource resource)
 {
     lock(_fileLock)
         return new FileStream(BasePath + resource.RelativePath, FileMode.Open, FileAccess.Read, FileShare.Read);
 }
Пример #11
0
        public byte[] GetBytesFromFile(Resource resource)
        {
            var fi = new FileInfo(BasePath + resource.RelativePath);
            var buffer = new byte[fi.Length];
            
            lock (_fileLock)
                using (var fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
                    fs.Read(buffer, 0, (int)fs.Length);

            return buffer;
        }
Пример #12
0
 public void RemoveFile(Resource resource)
 {
     _files.Remove(resource.Id);
     File.Delete(BasePath+resource.RelativePath);
     if (FileRemoved != null)
         FileRemoved(this, new FileEventArgs(resource));
     Log.Out("FileStore", string.Format("FileStore: Removed file {0} from store", resource.Name), LogCode.Log);
 }
Пример #13
0
        public void UpdateFile(Resource resource, byte[] fileInBytes, FileSource source)
        {
            SaveToDisk(fileInBytes, resource);
            _files[resource.Id] = resource;

            switch (source)
            {
                case FileSource.ActivityCloud:
                    if (FileDownloadedFromCloud != null)
                        FileDownloadedFromCloud(this, new FileEventArgs(resource));
                    break;
                case FileSource.ActivityManager:
                    if (FileChanged != null)
                        FileChanged(this, new FileEventArgs(resource));
                    break;
            }
            Log.Out("FileStore", string.Format("Updated file {0} to store", resource.Name), LogCode.Log);
        }
Пример #14
0
 public void UpdateFile(Resource resource,Stream stream,FileSource source)
 {
     UpdateFile(resource, GetBytesFromStream(resource, stream), source);
 }
 public void DeleteFile(Resource resource)
 {
     //File.Delete(resource.RelativePath);
 }
Пример #16
0
 private void Check(Resource resource, byte[] fileInBytes)
 {
     if (_files == null)
         throw new Exception("Filestore: Not initialized");
     if (resource == null)
         throw new Exception(("Filestore: Resource not found"));
     if (fileInBytes == null)
         throw new Exception(("Filestore: Bytearray null"));
     if (fileInBytes.Length == 0)
         throw new Exception(("Filestore: Bytearray empty"));
 }
Пример #17
0
 public virtual void FileNetDeleteRequest(Resource r)
 {
     if (FileDeleteRequest != null)
         FileDeleteRequest(this, new FileEventArgs(r));
 }
Пример #18
0
        private void SaveToDisk(byte[] fileInBytes, Resource resource)
        {
            var path = Path.Combine(BasePath, resource.RelativePath);
            var dir = Path.GetDirectoryName(path);
            if (dir != null && !Directory.Exists(dir)) Directory.CreateDirectory(dir);

            lock (_fileLock)
            {
                if(!File.Exists(@path))
                {
                    using (var fileToupload = new FileStream(@path, FileMode.OpenOrCreate))
                    {
                        fileToupload.Write(fileInBytes, 0, fileInBytes.Length);
                        fileToupload.Close();
                        fileToupload.Dispose();

                        //File.SetCreationTimeUtc(path, DateTime.Parse(resource.CreationTime));
                        //File.SetLastWriteTimeUtc(path, DateTime.Parse(resource.LastWriteTime));
                        Console.WriteLine("FileStore: Saved file {0} to disk at {1}", resource.Name,
                                          path);
                        Log.Out("FileStore", string.Format("FileStore: Saved file {0} to disk at {1}", resource.Name,
                                          path), LogCode.Log);
                    }
                }
                else
                    Log.Out("FileStore", string.Format("FileStore: file {0} already in store", resource.Name,
                                          path), LogCode.Log);

            }
        }
Пример #19
0
        /// <summary>
        /// Adds a file to a given activity
        /// </summary>
        /// <param name="fileInfo">The fileinfo describing the file</param>
        /// <param name="activityId">The id of activity</param>
        public void AddResource(FileInfo fileInfo,Guid activityId)
        {
            //Create a new resource from the file
            var resource = new Resource((int)fileInfo.Length, fileInfo.Name)
            {
                ActivityId = activityId,
                CreationTime = DateTime.Now.ToString("u"),
                LastWriteTime = DateTime.Now.ToString("u")  
            };
            var req = new FileRequest
                          {
                              Resouce = resource,
                              Bytes = JsonConvert.SerializeObject(File.ReadAllBytes(fileInfo.FullName))
                          };

            Task.Factory.StartNew(
                delegate
                {
                        Rest.Post(ServiceAddress + Url.Files, req);
                        Log.Out("ActivityClient", string.Format("Received Request to upload {0}", resource.Name),
                                LogCode.Log);
                    });

        }
Пример #20
0
 public DownloadState(Resource resource, FileSource fileSource)
 {
     Resource = resource;
     FileSource = fileSource;
 }
Пример #21
0
 /// <summary>
 /// Removes a file from the File server
 /// </summary>
 /// <param name="resource">Resource that represents the file</param>
 public void RemoveFile(Resource resource)
 {
  Task.Factory.StartNew(
         delegate
             {
                 _fileServer.RemoveFile(resource);
                 Console.WriteLine("ActivityManager: Deleted file {0}", resource.Name);
             });
 }
Пример #22
0
 private static string GenerateId(Resource r)
 {
     return r.ActivityId + "/" + r.Id;
 }