Rub21 left a comment (openstreetmap/openstreetmap-website#4188)

I had a look on the code, 
https://github.com/openstreetmap/openstreetmap-website/blob/a1e0b7faa4eac18db7841f90d77ef2b106350630/app/models/trace.rb#L76-L86

[content_type](https://github.com/openstreetmap/openstreetmap-website/blob/a1e0b7faa4eac18db7841f90d77ef2b106350630/app/models/trace.rb#L280-L296)
  already detects application/gpx+xml, so we can gzip those into a Tempfile 
before calling super.  and for files that arrive already compressed (gzip, 
bzip2, zip, tar.*) we leave as they are now.

Here is how the function file could be changed

```rd
def file=(attachable)
  case attachable
  when ActionDispatch::Http::UploadedFile, Rack::Test::UploadedFile
    type = content_type(attachable.path)

    if type == "application/gpx+xml"
      # compres  gpx+xm file  to .gpx.gz
      gz = Tempfile.new(["trace", ".gpx.gz"])
      Zlib::GzipWriter.open(gz.path) do |w|
        IO.copy_stream(attachable.path, w)
      end

      super(:io => File.open(gz.path),
            :filename => "#{attachable.original_filename}.gz",
            :content_type => "application/gzip",
            :identify => false)
    else
      # Same as it is now
      super(:io => attachable,
            :filename => attachable.original_filename,
            :content_type => type,
            :identify => false)
    end
  else
    super
  end
end


```

The change only touches active_storage_blobs (the physical file on S3 and its 
content_type). The user-visible gpx_files.name is not affected by the 
compression step itself.


- **Reading and API downloads keep working unchanged**: 
[xml_file](https://github.com/openstreetmap/openstreetmap-website/blob/a1e0b7faa4eac18db7841f90d77ef2b106350630/app/models/trace.rb#L168-L199)
  already runs gunzip -c for application/gzip, so JOSM, iD and any API consumer 
asking for .xml or .gpx will keep receiving plain XML on the fly, same as today.


- The only change is the raw download button on the website,  a user that 
uploaded a plain .gpx and clicks "Download" will get <id>.gpx.gz instead of 
<id>.gpx.  it seems this is already the current behaviour for users that upload 
.gpx.gz directly, and it raises  two questions?

     - keep the original name: gpx_files.name stays as `my_route.gpx`, even 
though the blob is now gzipped. The compression is invisible in the UI.
     - reflect the compression in the website: rename gpx_files.name to 
`my_route.gpx.gz`, the same way it already shows today for users that upload 
.gpx.gz directly. We could add a small note in the website  that saids: "Your 
file will be compressed before storage".



-- 
Reply to this email directly or view it on GitHub:
https://github.com/openstreetmap/openstreetmap-website/issues/4188#issuecomment-4399134611
You are receiving this because you are subscribed to this thread.

Message ID: 
<openstreetmap/openstreetmap-website/issues/4188/[email protected]>
_______________________________________________
rails-dev mailing list
[email protected]
https://lists.openstreetmap.org/listinfo/rails-dev

Reply via email to