anchor.diff:                  ArchiveFile = z_media_archive:archive_copy_opt(File, SafeFilename, Context),
                   RootName = filename:rootname(filename:basename(ArchiveFile)),
                   MediumRowProps = [
  -                    {id, RscId}, 
                       {filename, ArchiveFile}, 
                       {rootname, RootName}, 
                       {is_deletable_file, not z_media_archive:is_archived(File, Context)}
                       | PropsMedia
                   ],
   
  -                %% When the resource is in the media category, then move it to the correct sub-category depending
  -                %% on the mime type of the uploaded file.
  -                case rsc_is_media_cat(RscId, Context) of
  -                    true -> m_rsc:update(RscId, [{category, mime_to_category(Mime)}], Context);
  -                    false -> m_rsc:touch(RscId, Context)
  -                end,
  -
                   F = fun(Ctx) ->
  -                    z_db:delete(medium, RscId, Context),
  -                    case z_db:insert(medium, MediumRowProps, Ctx) of
  -                        {ok, _MediaId} ->
  -                            {ok, RscId};
  -                        Error ->
  -                            Error
  -                    end
  -                end,
  -
  -                Depicts = depicts(RscId, Context),
  +                            {ok, Id} = case RscId of
  +                                           insert_rsc ->
  +                                               m_rsc:insert([{category, mime_to_category(Mime)} | Props], Ctx);
  +                                           _ ->
  +                                               %% When the resource is in the media category, then move it to the correct sub-category depending
  +                                               %% on the mime type of the uploaded file.
  +                                               case rsc_is_media_cat(RscId, Context) of
  +                                                   true -> m_rsc:update(RscId, [{category, mime_to_category(Mime)}], Context);
  +                                                   false -> m_rsc:touch(RscId, Context)
  +                                               end,
  +                                               z_db:delete(medium, RscId, Context),
  +                                               {ok, RscId}
  +                                       end,
  +                            case z_db:insert(medium, [{id, Id} | MediumRowProps], Ctx) of
  +                                {ok, _MediaId} ->
  +                                    {ok, Id};
  +                                Error ->
  +                                    Error
  +                            end
  +                    end,
  +                
                   {ok, Id} = z_db:transaction(F, Context),
  +                Depicts = depicts(Id, Context),
                   [ z_depcache:flush(DepictId, Context) || DepictId <- Depicts ],
                   z_depcache:flush(Id, Context),
  +
  +                %% Flush categories
  +                CatList = m_rsc:is_a(Id, Context),
  +                [ z_depcache:flush(Cat, Context) || Cat <- CatList ],
  +                
                   m_rsc:get(Id, Context), %% Prevent side effect that empty things are cached?
                   %% Pass the medium record along in the notification; this also fills the depcache (side effect).
                   z_notifier:notify(#media_replace_file{id=Id, medium=m_media:get(Id, Context)}, Context),
  
positive.code: 
      %% @doc Replace the file, no mime check needed.
      replace_file_mime_ok(File, RscId, Props, PropsMedia, Context) ->
          case z_acl:rsc_editable(RscId, Context) orelse not(m_rsc:p(RscId, is_authoritative, Context)) of
              true ->
                  Mime = proplists:get_value(mime, PropsMedia),
                  SafeRootName = z_string:to_rootname(proplists:get_value(original_filename, Props, File)),
  
negative.code:         undefined -> [{title, proplists:get_value(original_filename, Props2)}|Props2];
          _ -> Props2
      end,
      replace_file_mime_ok(File, insert_rsc, Props3, PropsMedia, Context).
  
  
  %% @doc Replaces a medium file, when the file is not in archive then a copy is made in the archive.
  