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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds a resource if a resource with the MD5 hash has not been added previously
        /// </summary>
        /// <param name="resource">resource to add</param>
        public virtual void AddResource(DigitalResource resource)
        {
            DigitalResource foundResource = (from r in Resources
                                             where r.Md5 == resource.Md5
                                             select r).FirstOrDefault();

            if (foundResource == null)
            {
                Resources.Add(resource);
            }
        }
        //Dont need this. Just need to add the tag to the resource model,
        //then save the resource
        //public int AddTagToResource(string md5, ResourceModel.Tag tag)
        //{
        //    throw new NotImplementedException();
        //}

        public int AddToAlbum(int id, ResourceModel.DigitalResource resource)
        {
            int   resourcesAdded = 0;
            Album album          = GetAlbums(x => x.ID == id).FirstOrDefault();

            if (album != null)
            {
                album.AddResource(resource);

                //save the updated album to the database


                resourcesAdded++;
            }
            return(resourcesAdded);
        }
        public void SaveResource(ResourceModel.DigitalResource resource)
        {
            var sessionFactory = SessionFactoryCreator.CreateSessionFactory();

            using (var session = sessionFactory.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    //try
                    {
                        session.SaveOrUpdate(resource);
                        session.Transaction.Commit();
                    }
                }
            }
        }
        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);
        }