Пример #1
0
        public async Task Serve(int id)
        {
            var media = await _mediaStore.GetByIdAsync(id);

            var r = Response;

            r.Clear();

            if ((media != null) && (media.ContentLength >= 0))
            {
                r.ContentType = media.ContentType;
                r.Headers.Add(HeaderNames.ContentDisposition, "filename=\"" + media.Name + "\"");
                r.Headers.Add(HeaderNames.ContentLength, Convert.ToString((long)media.ContentLength));
                r.Headers.Add(HeaderNames.CacheControl, "public,max-age=7776000"); // 7776000 = 90 days
                await r.Body.WriteAsync(media.ContentBlob, 0, (int)media.ContentLength);
            }
            else
            {
                var fileBytes = await _fileStore.GetFileBytesAsync(_pathToEmptyImage);

                if (fileBytes != null)
                {
                    r.ContentType = "image/png";
                    r.Headers.Add(HeaderNames.ContentDisposition, "filename=\"empty.png\"");
                    r.Headers.Add(HeaderNames.ContentLength, Convert.ToString((int)fileBytes.Length));
                    r.Headers.Add(HeaderNames.CacheControl, "public,max-age=7776000"); // 7776000 = 90 days
                    await r.Body.WriteAsync(fileBytes, 0, fileBytes.Length);
                }
            }
        }
Пример #2
0
        public async Task Serve(int id)
        {
            var media = await _mediaStore.GetByIdAsync(id);

            var r = Response;

            r.Clear();
            if ((media != null) && (media.ContentLength >= 0))
            {
                r.ContentType = media.ContentType;
                r.Headers.Add("content-disposition", "filename=\"" + media.Name + "\"");
                r.Headers.Add("content-length", Convert.ToString((long)media.ContentLength));
                r.Body.Write(media.ContentBlob, 0, (int)media.ContentLength);
            }
            else
            {
                var fileBytes = await _fileStore.GetFileBytesAsync(_pathToEmptyImage);

                if (fileBytes != null)
                {
                    r.ContentType = "image/png";
                    r.Headers.Add("content-disposition", "filename=\"empty.png\"");
                    r.Headers.Add("content-length", Convert.ToString((int)fileBytes.Length));
                    r.Body.Write(fileBytes, 0, fileBytes.Length);
                }
            }
        }
Пример #3
0
        public async Task ToggleFavoriteAsync(Guid id, bool isFavorite, CancellationToken cancellationToken)
        {
            Media media = await _mediaStore.GetByIdAsync(id, cancellationToken);

            media.IsFavorite = isFavorite;

            await _mediaStore.UpdateAsync(media, cancellationToken);

            await _bus.Publish(new FavoriteMediaToggledMessage(id, isFavorite));
        }
Пример #4
0
        public async Task <IActionResult> GetById(Guid id)
        {
            var entry = await _store.GetByIdAsync(id, CancellationToken);

            if (entry.Length == 0)
            {
                return(NotFound());
            }

            return(File(entry.Data, entry.Type));
        }
Пример #5
0
        private async Task <MediaOperationCompletedMessage> MoveMediaAsync(
            Guid id,
            string newLocation,
            CancellationToken cancellationToken)
        {
            Media media = await _mediaStore.GetByIdAsync(id, cancellationToken);

            MediaOperationCompletedMessage msg = new MediaOperationCompletedMessage
            {
                MediaId = id,
                Data    = new() { ["OldFolder"] = media.Folder, ["NewFolder"] = newLocation },
Пример #6
0
        public async Task <MediaStream> GetVideoAsync(Guid id, CancellationToken cancellationToken)
        {
            Media?video = await _mediaStore.GetByIdAsync(id, cancellationToken);

            MediaBlobData request = _mediaService.GetBlobRequest(
                video,
                MediaFileType.Video720);

            Stream stream = _mediaStore.Blob.GetStreamAsync(request);

            return(new MediaStream(stream, video.Filename.Split('.').Last()));
        }
Пример #7
0
        private async Task UpdateAgeAsync(
            MediaFace face,
            Person person,
            CancellationToken cancellationToken)
        {
            if (person.DateOfBirth.HasValue)
            {
                Media media = await _mediaStore.GetByIdAsync(face.MediaId, cancellationToken);

                face.Age = _ageOperationsService.CalculateAge(
                    media.DateTaken,
                    person.DateOfBirth.Value);
            }
            else
            {
                face.Age = null;
            }
        }
Пример #8
0
        private async Task <MediaOperationCompletedMessage> UpdateMetadataAsync(
            Guid id,
            DateTimeOffset?dateTaken,
            UpdateMedataGeoLocation?geoLocation,
            CancellationToken cancellationToken)
        {
            MediaOperationCompletedMessage msg = new MediaOperationCompletedMessage
            {
                Type    = MediaOperationType.UpdateMetadata,
                MediaId = id,
            };

            try
            {
                if (dateTaken.HasValue)
                {
                    await _mediaService.UpdateDateTakenAsync(id, dateTaken, cancellationToken);
                }
                if (geoLocation is { } geo&& geo.Latitude.HasValue && geo.Longitude.HasValue)
                {
                    Media media = await _mediaStore.GetByIdAsync(id, cancellationToken);

                    media.GeoLocation = new GeoLocation
                    {
                        Point   = GeoPoint.Create(geo.Latitude.Value, geo.Longitude.Value),
                        Type    = "User",
                        GeoHash = GeoHash.Encode(geo.Latitude.Value, geo.Longitude.Value),
                        Address = GetAddress(geoLocation)
                    };

                    await _mediaStore.UpdateAsync(media, cancellationToken);
                }

                msg.IsSuccess = true;
            }
            catch (Exception ex)
            {
                msg.IsSuccess = false;
                msg.Message   = ex.Message;
            }

            return(msg);
        }
Пример #9
0
        private async Task <MediaOperationCompletedMessage> RecycleAsync(
            Guid id,
            CancellationToken cancellationToken)
        {
            Media media = await _mediaStore.GetByIdAsync(id, cancellationToken);

            MediaOperationCompletedMessage msg = new MediaOperationCompletedMessage
            {
                Type    = MediaOperationType.Recycle,
                MediaId = id,
            };

            try
            {
                await _mediaBlobStore.MoveToSpecialFolderAsync(
                    new MediaBlobData
                {
                    Directory = media.Folder,
                    Filename  = media.Filename,
                    Type      = MediaBlobType.Media
                },
                    MediaBlobType.Recycled,
                    cancellationToken);

                media.Folder = null;
                media.State  = MediaState.Recycled;

                await _mediaStore.UpdateAsync(media, cancellationToken);

                msg.Message = $"{media.Filename} Recycled";

                await RecycleFacesAsync(media.Id, cancellationToken);

                msg.IsSuccess = true;
            }
            catch (Exception ex)
            {
                msg.IsSuccess = false;
                msg.Message   = ex.Message;
            }

            return(msg);
        }
Пример #10
0
 public async Task <Media> GetByIdAsync(Guid id, CancellationToken cancellationToken)
 {
     return(await _mediaStore.GetByIdAsync(id, cancellationToken));
 }