private static DigitalResource CreateNewResourceObject(ResourceModel.User owner, Stream fileStream, string originalName, ResourceType imageRT, ResourceType otherRT, string md5Sum) { DigitalResource resource = new ResourceModel.DigitalResource { Md5 = md5Sum, OriginalFileName = originalName, Size = fileStream.Length, Uploaded = DateTime.Now, Owners = new List <ResourceModel.User>() }; resource.Owners.Add(owner); if (ReferenceService.IsValidImage(fileStream)) { resource.Type = imageRT; //resource.Type = "Image"; resource.Date = ReferenceService.GetDateTakenFromImage(fileStream); } else { resource.Type = otherRT; resource.Date = null; } return(resource); }
/// <summary> /// Get the specified file. This is restricted by user as files can be confidential /// </summary> /// <param name="id"></param> /// <param name="user"></param> /// <returns></returns> public DigitalResource Get(string id, ResourceModel.User user) { DigitalResource resource;// list = new List<Resource>(); var sessionFactory = SessionFactoryCreator.CreateSessionFactory(); using (var session = sessionFactory.OpenSession()) { using (session.BeginTransaction()) { //session.CreateCriteria(typeof(DigitalResource)).ToList(); resource = (from res in session.Query <DigitalResource>() where id == res.Md5 && res.Owners.Any(x => user.UserName == x.UserName) select res).FirstOrDefault(); if (resource != null) { IAmazonS3 s3Client = new AmazonS3Client(); long size = s3Client.GetObjectMetadata("piccoli", id).ContentLength; resource.Size = size; } } } return(resource); }
private static DigitalResource CreateNewResourceObject(ResourceModel.User owner, long size, string originalName, ResourceType imageRT, ResourceType otherRT, string md5Sum) { DigitalResource resource = new ResourceModel.DigitalResource { Md5 = md5Sum, OriginalFileName = originalName, Size = size, Uploaded = DateTime.Now, Owners = new List <ResourceModel.User>() }; resource.Owners.Add(owner); //assume that resource type isn't an image. resource.Type = otherRT; resource.Date = null; return(resource); }
public string CompletePartialAndSaveResource(ReferenceRepository referenceRepository, ResourceModel.User owner, string uploadIdentifier, String originalName, String md5OfResource, List <string> etags, long totalSize) { IAmazonS3 s3Client = new AmazonS3Client(); ResourceModel.DigitalResource resource = null; ResourceType imageRT = referenceRepository.AllResourceTypes().Where(x => x.Type == "Image").FirstOrDefault(); ResourceType otherRT = referenceRepository.AllResourceTypes().Where(x => x.Type == "Other").FirstOrDefault(); List <PartETag> partTags = new List <PartETag>(); int partNumber = 1; foreach (string tag in etags) { partTags.Add(new PartETag { ETag = tag, PartNumber = partNumber }); partNumber++; } // complete the upload. CompleteMultipartUploadRequest completeRequest = new CompleteMultipartUploadRequest { BucketName = "piccoli", Key = md5OfResource, UploadId = uploadIdentifier, PartETags = partTags }; // Complete the upload. CompleteMultipartUploadResponse completeUploadResponse = s3Client.CompleteMultipartUpload(completeRequest); //create the resource object resource = CreateNewResourceObject(owner, totalSize, originalName, imageRT, otherRT, md5OfResource); SaveResource(resource);//update the database return(completeUploadResponse.ETag); }
/// <summary> /// Saves a NEW resource. Only saves if resource with the MD5Sum has not previously been added. /// </summary> /// <param name="referenceRepository"></param> /// <param name="fileStream"></param> /// <param name="originalName"></param> /// <returns>the AWS Upload ID</returns> public async Task <string> UploadPartial(ResourceModel.User owner, string md5OfResource, string uploadIdentifier, Stream fileStream, int partNumber, int numberOfParts) { ResourceModel.DigitalResource resource = null; //User user = reference try { //bool exists = Exists(md5Sum); DigitalResource existingResource = Get(md5OfResource); if (existingResource == null) { //fileStream.Position = 0; //TransferUtilityUploadRequest tuu = new TransferUtilityUploadRequest //{ // InputStream = fileStream, // BucketName = "piccoli", // Key = "belvedere" //}; //tr.UploadAsync(tuu); //upload the file IAmazonS3 s3Client = new AmazonS3Client(); //ListBucketsResponse response = s3Client.ListBuckets(); //https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/S3/TUploadPartResponse.html //https://docs.aws.amazon.com/AmazonS3/latest/dev/LLuploadFileDotNet.html string uploadID = uploadIdentifier; if (partNumber == 1) { InitiateMultipartUploadRequest initiateRequest = new InitiateMultipartUploadRequest { BucketName = "piccoli", Key = md5OfResource, }; // Initiate the upload. InitiateMultipartUploadResponse initResponse = await s3Client.InitiateMultipartUploadAsync(initiateRequest); uploadID = initResponse.UploadId; } // Upload part X UploadPartRequest uploadRequest = new UploadPartRequest { BucketName = "piccoli", Key = md5OfResource, UploadId = uploadID, PartNumber = partNumber, PartSize = fileStream.Length, //5242880,//5MB ? InputStream = fileStream }; UploadPartResponse response = s3Client.UploadPart(uploadRequest); return(uploadID); } return("resource already exists"); } catch (AmazonS3Exception ex) { throw ex; } }
/// <summary> /// Saves a NEW resource. Only saves if resource with the MD5Sum has not previously been added. /// </summary> /// <param name="referenceRepository"></param> /// <param name="fileStream"></param> /// <param name="originalName"></param> /// <returns>DigitalResource with the length of the file. The Digital Resource may /// be newly created OR retrieved from the database if it already exists</returns> public ResourceModel.DigitalResource SaveOrGet(ReferenceRepository referenceRepository, ResourceModel.User owner, Stream fileStream, String originalName) { ResourceModel.DigitalResource resource = null; ResourceType imageRT = referenceRepository.AllResourceTypes().Where(x => x.Type == "Image").FirstOrDefault(); ResourceType otherRT = referenceRepository.AllResourceTypes().Where(x => x.Type == "Other").FirstOrDefault(); //User user = reference try { //calculate md5 of the file to upload string md5Sum = Md5Hash(fileStream); //bool exists = Exists(md5Sum); DigitalResource existingResource = Get(md5Sum); if (existingResource == null) { //create the resource object resource = new ResourceModel.DigitalResource { Md5 = md5Sum, OriginalFileName = originalName, Size = fileStream.Length, Uploaded = DateTime.Now, Owners = new List <ResourceModel.User>() }; resource.Owners.Add(owner); if (ReferenceService.IsValidImage(fileStream)) { resource.Type = imageRT; //resource.Type = "Image"; resource.Date = ReferenceService.GetDateTakenFromImage(fileStream); } else { resource.Type = otherRT; resource.Date = null; } //fileStream.Position = 0; //TransferUtilityUploadRequest tuu = new TransferUtilityUploadRequest //{ // InputStream = fileStream, // BucketName = "piccoli", // Key = "belvedere" //}; //tr.UploadAsync(tuu); //upload the file IAmazonS3 s3Client = new AmazonS3Client(); //ListBucketsResponse response = s3Client.ListBuckets(); using (TransferUtility tr = new TransferUtility(s3Client)) { tr.Upload(fileStream, "piccoli", md5Sum); //update the database SaveResource(resource); } } else { //another user also has a copy of this file if (existingResource.Owners.Where(x => x.UserName == owner.UserName).FirstOrDefault() == null) { existingResource.Owners.Add(owner); SaveResource(existingResource); } return(existingResource); } } catch (AmazonS3Exception ex) { throw ex; } return(resource); }
public bool ResourceExists(String hashID, ResourceModel.User user) { DigitalResource resource = Get(hashID, user); return(resource != null); }